如果所有玩家在回合中被杀死或重置,您如何停止游戏并向所有人显示消息?

时间:2019-05-16 04:40:29

标签: lua roblox

我正在开发一款重新制作的游戏,希望弄清楚如果所有玩家被杀或重置后回合将如何停止,并向所有玩家显示一条消息“游戏结束”,然后返回大厅休息。

这是我正在制作的游戏的主要脚本。过去,我曾尝试在此脚本中使用If语句,但是它没有用,最终也没有显示我的其他脚本。

local s = script.Stat

t = 0

while true do

    local plrs = game.Players:GetPlayers()

    t = 15  
    repeat
    t = t - 1
    s.Value = t.." seconds left"
    wait(1)

    if plrs == 0 then
        s.Value = "Alive: "..plrs
    end

    until t == 0

    s.Value = "Game over!"

    wait(5)

end

我希望脚本能在一轮被杀死的游戏中找到所有玩家,但是输出似乎只在剩余的几秒钟内起作用。

1 个答案:

答案 0 :(得分:0)

如果在每个玩家的Humanoid.Died信号中添加一个侦听器,则可以跟踪一轮中哪些玩家死亡。

在脚本中,编写如下内容:

-- use a map of player names to keep track of who is still alive
local playersAlive = {}
local listenerTokens = {}
local SPAWN_LOCATION = CFrame.new()

-- make some helper functions
local function getCountPlayersAlive()
    local numPlayersAlive = 0
    for playerName, isAlive in pairs(playersAlive) do
        numPlayersAlive = numPlayersAlive + 1
    end

    return numPlayersAlive
end

local function beginRound()    
    -- get the list of all of the players in the round
    local allPlayers = game.Players:GetChildren()
    for _, player in ipairs(allPlayers) do

        -- find the associated Player in the workspace
        local character = game.Workspace:FindFirstChild(player.Name)
        if character then

            -- add an event listener for when they die
            local humanoid = character:FindFirstChildOfClass("Humanoid")
            local deathSignalToken = humanoid.Died:Connect(function()

                -- when this player dies, signal that there is one less player
                playersAlive[player.Name] = nil
            end)

            -- add the player to the list of players for this round
            playersAlive[player.Name] = true

            -- move the players into the game arena
            player.Character:SetPrimaryPartCFrame(SPAWN_LOCATION)

            -- disconnect this signal at the end of the round
            table.insert(listenerTokens, deathSignalToken)

        else
            -- could not find the player, make them sit out this round
        end
    end
end

local function endRound()
    -- clean up the game board, reload the players, do whatever you have to

    -- clean up any death signals from last round
    for _, token in ipairs(listenerTokens) do
        token:Disconnect()
    end
end


-- set up the game loop
local GAME_LENGTH = 30 -- seconds
local INTERMISSION_LENGTH = 15 -- seconds

spawn(function()
    while true do
        -- phase 1 - set up the round
        beginRound()

        -- phase 2 - play the round
        local gameTimer = GAME_LENGTH
        while gameTimer > 0 and getCountPlayersAlive() > 0 do
            local count = getCountPlayersAlive()
            print(string.format("Time Left : %d - Players Left : %d", gameTimer, count))
            wait(1)
            gameTimer = gameTimer - 1
        end

        -- phase 3 - celebrate winners and clean up the previous round
        endRound()

        -- phase 4 - intermission before the next round
        wait(INTERMISSION_LENGTH)
    end
end)

抱歉,冗长的示例,但是希望对您有所帮助。