如何解决Roblox无穷大错误?

时间:2019-11-13 05:27:54

标签: lua roblox

<---This is my current local script--->

local replicatedStorage = game:GetService("ReplicatedStorage")
local starterRebirthAmount = 5000

local player = game.Players.LocalPlayer
local mainFrame = script.Parent:WaitForChild("MainFrame")
local rebirthMenu = mainFrame:WaitForChild("RebirthMenu")
local mainButton = script.Parent:WaitForChild("MainButton")
local rebirthButton = rebirthMenu:WaitForChild("RebirthButton")
local strengthToRebirth = rebirthMenu:WaitForChild("StrengthToRebirth")
local rebirths = player:WaitForChild("leaderstats").Rebirths <--The Bit I think is causing the 
problems.

strengthToRebirth.Text = "You need at least "..math.floor((starterRebirthAmount + (rebirths.Value) * 
math.sqrt(500000))).." strength to rebirth"

mainButton.MouseButton1Click:Connect(function()

mainFrame.Visible =  not mainFrame.Visible

end)

rebirthButton.MouseButton1Click:Connect(function()
local result = replicatedStorage.Remotes.Rebirth:InvokeServer()
if result == true then
rebirthButton.Text  = "Successfully Rebirthed"
wait(1)
rebirthButton.Text = "CLICK HERE TO REBIRTH"
elseif result == "NotEnoughStrength" then 
rebirthButton.Text = "Not Strong Enough!"
wait(1)
rebirthButton.Text = "CLICK HERE TO REBIRTH"

end
end)

rebirths:GetPropertyChangedSignal("Value"):Connect(function()

strengthToRebirth.Text = "You need at least "..math.floor((starterRebirthAmount + (rebirths.Value) * 
math.sqrt(5000000))).." strength to rebirth" end)

我每次都会遇到相同的错误。 21:10:20.963-

  

可能产生无限大的收益   'Players.Archerofcool:WaitForChild(“ leaderstats”)'

2 个答案:

答案 0 :(得分:1)

在使用任何功能之前,请先阅读文档!

来自https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

  

WaitForChild

     

如果对该函数的调用超过5秒而没有返回,并且没有   已指定timeOut参数,警告将被打印到   输出线程可能无限期产生的结果;这个警告需要   在'X:WaitForChild(“ Y”)'上可能形成无限的收益,其中X是   父名称,Y是子对象名称。

答案 1 :(得分:0)

WaitForChild()如果您需要等待事物(例如播放器及其外观)载入世界,那就很好了。但是正如Piglet指出的那样,不提供超时将导致它引发类似您所看到的错误。

提供超时后,该函数将返回nil,并且您需要能够对此情况做出反应。

如果您确定在脚本执行时已加载所有内容,则可以像抓取普通索引一样简单地抓取所有内容。

-- wait for the stuff to load...
while script.Parent:WaitForChild("MainFrame", 1) == nil do
    wait(1)
end

-- grab all the elements
local mainButton = script.Parent.MainButton
local mainFrame = script.Parent.MainFrame
local rebirthMenu = mainFrame.RebirthMenu
local rebirthButton =  rebirthMenu.RebirthButton
local strengthToRebirth = rebirthMenu.StrengthToRebirth

-- wait for the player to load in
local player = game.Players.LocalPlayer
while player:WaitForChild("leaderstats", 1) == nil do
    wait(1)
end
local rebirths = player.leaderstats.Rebirths