如何修复无限 yield 可能性?

以下代码位于驾驶座内的脚本中,当玩家坐在座位上时,会输出以下信息

"Infinite yield possible on 'Workspace.Car4.DriveSeat:WaitForChild("Humanoid")"

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function onSeated(isSeated,Seat)
    if humanoid ~= nil then
    local p = game.Players:FindFirstChild(n.Name)
        if p.Team.Name == "Thieves" then
            game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
        end
    end
end
humanoid.Seated:Connect(onSeated(isSeated, Seat))
点赞
用户2858170
用户2858170

你可以参考Roblox手册,找到以下内容:

Instance:WaitForChild(childName, timeOUt)

返回具有给定名称的实例的子对象。如果子对象不存在,它会暂停当前线程,直到它被找到。如果指定了timeOut参数,当在超过timeOut秒的时间内未能找到该子对象时,此函数将返回nil并超时。

如果对此函数的调用超过5秒而未返回任何结果,并且未指定timeOut参数,则会将一个警告打印到输出界面,提示该线程可能会无限挂起。该警告的形式是:“Infinite yield possible on 'X:WaitForChild("Y")'”,其中X是父对象名称,Y是子对象名称。

因此,要避免此警告,可以为timeOut提供一个值,或确保子对象在5秒内被找到。

2021-05-10 13:29:05