在 'Players.shaz:WaitForChild("Values")' 上可能会产生无限收益,我正在努力做到这一点,所以当玩家认领摊位时,它会将 bool 值变为 true

infinite yield possible on 'Players.shaz:WaitForChild("Values")' im trying to make it so when the player claims the booth it turns bool value to true

local claim = script.Parent.Parent.Parent.Parent.StandMesh.Claim

This will trigger the Proximty Prompt.

claim.Triggered:Connect(function(player)
local screengui = player.PlayerGui:WaitForChild('Popups')
local popup = player.PlayerGui:WaitForChild('Popups').ClaimedBooth
local bool = player:WaitForChild('Values').Claimed

“无限产量可能”是一个警告。它让您知道您的代码是不安全的,并且当此代码执行时,Values 对象不存在(或拼写错误)超过 5 秒,并且 player:WaitForChild('Values') 无法解析为一个对象。

Instance:WaitForChild() 的文档是这样说的:

Notes

  • If a call to this function exceeds 5 seconds without returning, and no timeOut parameter has been specified, a warning will be printed to the output that the thread may yield indefinitely; this warning takes the form Infinite yield possible on 'X:WaitForChild("Y")', where X is the parent name and Y is the child object name.
  • This function does not yield if a child with the given name exists when the call is made.
  • This function is less efficient than Instance:FindFirstChild or the dot operator. Therefore, it should only be used when the developer is not sure if the object has replicated to the client. Generally this is only the first time the object is accessed

您可以通过添加 timeout 作为第二个参数来删除此警告,但您需要考虑到可能找不到该对象这一事实:

local timeout = 2 -- seconds
local values = player:WaitForChild('Values', timeout)
if values then
    local boolVal = values.Claimed
    print("Claimed = ", boolVal.Value)
    -- mark it as claimed
    boolVal.Value = true
else
    warn("Could not find 'Values' BoolValue")
end

或者,如果您知道 Values 将在此代码执行时存在,您可以简单地使用点运算符或 FindFirstChild() 函数访问它。但如果 Values 不存在,这将引发“尝试索引 nil”错误。

local boolVal = player:FindFirstChild('Values').Claimed
-- or
local boolVal = player.Values.Claimed

-- mark it as claimed
boolVal.Value = true

当您看到此警告时,您应该仔细检查对象名称的拼写以及脚本执行的时间,以确保该对象确实存在。