我在 Lua 的 While 循环中不断收到无法解释的错误
I keep getting an unexplained error in my While loop in Lua
我是 lua 的新手,但对编码并不陌生,我只是想编写一些代码来更改砖块的透明度,作为 lua 中的第一个项目。但是每当我 运行 代码时,我的 while 循环中都会出现无法解释的错误。我已经尝试多次更改循环内的所有内容,但它仍然只给我这个错误。
这是代码:
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
while true do
value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
end
代码应该不断改变对象的透明度,但它只是在第 3 行给我一个错误
(下面是它给我的错误截图)
https://i.stack.imgur.com/0TnYD.png
您的脚本可能由于无限循环而超时。它作为服务器的“设置”执行,因此您实际上看不到它执行任何操作。
您最好通过其他一些方法(例如触摸事件)来触发透明度更改来说明它。
例如:
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
game.Workspace.Brick.Touched:Connect(function()
local value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
end)
您超时了。你可以用一个简单的 wait()
来解决这个问题。没有它这将永远无法工作,因为 while 循环 运行 非常快。下面附上代码。
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
while true do
value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
wait()
end
正如其他人也指出的那样,问题是您的 while 循环永远不会产生,因此脚本超时。在循环中添加一个 wait()
可以解决问题。
但是您使用 while 循环的全部原因是为了制作闪烁效果的动画。与其使用 while 循环来跳过动画值,更强大的解决方案是使用 TweenService 来设置动画值。如果您知道起始值和结束值,TweenService 将为您创建中间“补间”值。而且,您可以指定 Tween 在完成后应该反转,这样您就可以获得 0 -> 1 -> 0 闪烁样式的动画。这使您可以专注于动画的外观,而不必担心从起点和终点进行插值的数学运算。
唯一棘手的部分是让它无限循环,但我们可以只监听动画何时结束,然后创建一个新动画。
尝试这样的事情:
local TweenService = game:GetService("TweenService")
-- find the brick
local Brick = game.Workspace.Brick
-- specify some details about how the animation should look
local animationInfo = TweenInfo.new(3.0, -- animation length (seconds)
Enum.EasingStyle.Linear, --how even is every step along the way?
Enum.EasingDirection.InOut, -- should large increments be frontloaded or backloaded (ignored when style is Linear)
0, -- repeat count
true, -- does it reverse when makes it to the end value?
0.0 -- delay (seconds)
)
-- specify all of the properties to animate
local propsToTween = {
Transparency = 1.0,
}
-- keep a reference to the currently running animation
local currentTween
local function createNewTween()
-- override the existing tween
currentTween = TweenService:Create(Brick, animationInfo, propsToTween)
-- listen for when the animation completes to loop infinitely
local animationCompletedConnection
animationCompletedConnection = currentTween.Completed:Connect(function(playbackState)
if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
-- remove this callback
animationCompletedConnection:Disconnect()
-- create a new tween to loop
createNewTween()
end
end)
-- play the animation
currentTween:Play()
end
-- start the loop
createNewTween()
我是 lua 的新手,但对编码并不陌生,我只是想编写一些代码来更改砖块的透明度,作为 lua 中的第一个项目。但是每当我 运行 代码时,我的 while 循环中都会出现无法解释的错误。我已经尝试多次更改循环内的所有内容,但它仍然只给我这个错误。
这是代码:
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
while true do
value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
end
代码应该不断改变对象的透明度,但它只是在第 3 行给我一个错误
(下面是它给我的错误截图)
https://i.stack.imgur.com/0TnYD.png
您的脚本可能由于无限循环而超时。它作为服务器的“设置”执行,因此您实际上看不到它执行任何操作。
您最好通过其他一些方法(例如触摸事件)来触发透明度更改来说明它。
例如:
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
game.Workspace.Brick.Touched:Connect(function()
local value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
end)
您超时了。你可以用一个简单的 wait()
来解决这个问题。没有它这将永远无法工作,因为 while 循环 运行 非常快。下面附上代码。
value_list = {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0}
index = 0
while true do
value = value_list[index]
game.Workspace.Brick.Transparency = value
if index == 21 then
index = 0
else
index += 1
end
wait()
end
正如其他人也指出的那样,问题是您的 while 循环永远不会产生,因此脚本超时。在循环中添加一个 wait()
可以解决问题。
但是您使用 while 循环的全部原因是为了制作闪烁效果的动画。与其使用 while 循环来跳过动画值,更强大的解决方案是使用 TweenService 来设置动画值。如果您知道起始值和结束值,TweenService 将为您创建中间“补间”值。而且,您可以指定 Tween 在完成后应该反转,这样您就可以获得 0 -> 1 -> 0 闪烁样式的动画。这使您可以专注于动画的外观,而不必担心从起点和终点进行插值的数学运算。
唯一棘手的部分是让它无限循环,但我们可以只监听动画何时结束,然后创建一个新动画。
尝试这样的事情:
local TweenService = game:GetService("TweenService")
-- find the brick
local Brick = game.Workspace.Brick
-- specify some details about how the animation should look
local animationInfo = TweenInfo.new(3.0, -- animation length (seconds)
Enum.EasingStyle.Linear, --how even is every step along the way?
Enum.EasingDirection.InOut, -- should large increments be frontloaded or backloaded (ignored when style is Linear)
0, -- repeat count
true, -- does it reverse when makes it to the end value?
0.0 -- delay (seconds)
)
-- specify all of the properties to animate
local propsToTween = {
Transparency = 1.0,
}
-- keep a reference to the currently running animation
local currentTween
local function createNewTween()
-- override the existing tween
currentTween = TweenService:Create(Brick, animationInfo, propsToTween)
-- listen for when the animation completes to loop infinitely
local animationCompletedConnection
animationCompletedConnection = currentTween.Completed:Connect(function(playbackState)
if playbackState == Enum.PlaybackState.Completed or playbackState == Enum.PlaybackState.Cancelled then
-- remove this callback
animationCompletedConnection:Disconnect()
-- create a new tween to loop
createNewTween()
end
end)
-- play the animation
currentTween:Play()
end
-- start the loop
createNewTween()