Roblox 中的褪色球 Lua

Fading Ball in Roblox Lua

我正在尝试制作一个渐变为 1 透明度然后又变回 0 的球。 这是我的代码:

ball = script.Parent
trans = 0
while true do 
if trans < 1 then
    while trans < 1 do
        ball.Transparency = trans
        wait(0.1)
        trans = trans + 0.1 
    end
end
if trans == 1 then
    while trans <= 1 and trans >=0 do
        ball.Transparency = trans 
        wait(0.1)
        trans = trans -0.1 
    end
end
end

球确实消失了,但再也没有回来。此时游戏将冻结。有什么解决办法吗?谢谢!

更新:所以我今天尝试了以下代码并且它工作正常但是当我在 if 语句中将 if ball.Transparency == 1 替换为 trans == 1 时,出现了同样的问题。请解释一下谢谢!

while true do
ball = script.Parent
trans = 0
for i=0, 1, 0.1 do
    trans = i
    wait(0.1)
    ball.Transparency = trans
end
if ball.Transparency == 1 then
    for i = 1, 0, -0.1 do
        trans = i
        wait(0.1)
        ball.Transparency = trans
    end
end
end

浮点精度:

0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1

等于

1

我不确定为什么当您将 trans 值分配给 ball.Transparency 时这不是一个因素。

两件事:

1) if tostring(trans) == “1.0” then 有效。

2) 更好的是:如果 trans == 1,为什么还要检查那里?当然会,因为前面的 for 循环保证了它。

此外,请小心使用 while true...这可能是您的程序“冻结”的原因。