【问题标题】:How can I wait in a repeat loop in Roblox Lua如何在 Roblox Lua 的重复循环中等待
【发布时间】:2021-12-31 02:06:07
【问题描述】:
game.Workspace.PetRooms.FireRoom.FireRoom.Floor.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.parent)
local char = hit.Parent -- Character
local hum = char:FindFirstChild("Humanoid") -- Humanoid
if hum then -- If humanoid then...
    if hum.Health ~= 0 and player.Team == game.Teams.Scientists then -- Makes sure that character is not dead; makes sure that character is a scientist
        repeat
            wait (10)
            hum.Health = hum.Health - 10 -- Kills the character slowly
        until hum.Health == 0
    end
    player.Team = game.Teams.Infected -- Changes the player's team to infected AFTER they die
end
end)

“wait(10)”应该在每个“- 10”生命值之间等待 10 秒,但代码只等待 10 秒然后迅速杀死玩家。

【问题讨论】:

  • 玩家一开始有多少生命值?
  • 拥有 100 点生命值

标签: lua wait repeat roblox


【解决方案1】:

您已将此回调连接到Touched 事件,并且每次玩家触摸该部件时都会触发该事件。如果玩家在地板上行走,则每次玩家的脚碰到它时都会开火。

可能发生的情况是这个事件被触发了多次,而你有一堆这样的重复循环同时运行,导致玩家的生命值迅速下降。

我建议取消连接,以便每个玩家只发生一个循环:

-- create a map of players touching the floor
local playersTouching = {}

local floor = game.Workspace.PetRooms.FireRoom.FireRoom.Floor
floor.Touched:Connect(function(hit)
    -- check that the thing that hit is a player
    local char = hit.Parent -- Character
    local hum = char:FindFirstChild("Humanoid") -- Humanoid
    if hum == nil then
        return
    end

    -- escape if this player is already touching the floor
    local playerName = char.Name
    if playersTouching[playerName] then
        return
    end

    -- the player was not touching before, so flip the debounce flag
    -- everything after this point happens once
    playerTouching[playerName] = true

    -- check if character is not dead and a scientist
    local player = game.Players:GetPlayerFromCharacter(hit.parent)
    local isScientist = player.Team == game.Teams.Scientists
    local isAlive = hum.Health > 0
    if isScientist and isAlive then
        -- start a loop to kill the player
        while hum.Health > 0 then
            wait(10)
            hum.Health = hum.Health - 10
        end

        -- Change the player's team to infected
        player.Team = game.Teams.Infected
    end

    -- clear the debounce flag
    playerTouching[playerName] = false
end)

【讨论】:

    【解决方案2】:

    除了GetPropertyChangedSignal,我真的想不出任何东西了 试试看吧

    【讨论】:

    • 你知道为什么脚本在所有 -10 生命值之间不等待(10),而是在等待 10 秒后 -10 生命值真的很快吗?我不明白。
    猜你喜欢
    • 2020-06-22
    • 2019-05-16
    • 2021-04-21
    • 1970-01-01
    • 2021-09-28
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    • 2019-05-22
    相关资源
    最近更新 更多