食物脚本在工作室中有效,但在游戏中无效

问题描述 投票:0回答:1

我一直遇到这个问题,我不知道如何解决,因为控制台没有显示任何错误,所以基本上在工作室中它通常会每隔一段时间拿走 10 份食物,但在游戏中却不会

local Datastoreservice = game:GetService("DataStoreService")
local datastore = Datastoreservice:GetDataStore("food")
print("Coucou toi !")
local debounce = false
game.Players.PlayerAdded:Connect(function(player)

    local Values = Instance.new("Folder")
    Values.Name = "Values"
    Values.Parent = player

    local food = Instance.new("NumberValue")
    food.Parent = Values
    food.Name = "Food"
    food.Value = 100

    userid = player.UserId

    local data
    local success, errormessage = pcall(function()
        data = datastore:GetAsync(userid)
    end)

    if success then
        if data then
            food.Value = data
        end
    else
        food.Value = 100
    end

    player.CharacterAdded:Connect(function()
        food.Value = 100
    end)
    while player.Character do
        food.Value = math.clamp(food.Value - 10, 0, 100)
        local hum = player.Character:WaitForChild("Humanoid")
        if food.Value == 0 then
            hum.Health -= 5
        else
            hum.Health += 5
        end
        wait(9)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local data = player.Values.Food.Value

    local success, errormessage = pcall(function()
        datastore:SetAsync(userid, data)
    end)

    if success then
        print("Player data saved.")
    else
        print("There was trouble.")
        warn(errormessage)
    end

end)

在第 31 行(当player.Character 这样做时),之前它只是一个 true 并且工作正常,直到我遇到错误:脚本超时:耗尽允许的执行时间

roblox
1个回答
0
投票

将循环移至CharacterAdded事件内,您可以检查玩家的生命值是否大于0,而不是检查

Player.Character
。当玩家在游戏中死亡时循环终止,并在他们重生时再次开始。

© www.soinside.com 2019 - 2024. All rights reserved.