如果所有玩家在一轮内被杀死或重置,如何停止游戏并向所有人显示一条消息?

我正在开发一款重制游戏,并想知道如果所有玩家都被杀死或重置,轮会如何停止,并向所有玩家显示"游戏结束"的消息,然后返回到大厅休息时间。

这是我正在制作的游戏的主要脚本。过去,我尝试在此脚本中使用If语句,但由于它没有运行起来,它最终也没有显示我的另一个脚本。

local s = script.Stat

t = 0

while true do

    local plrs = game.Players:GetPlayers()

    t = 15
    repeat
    t = t - 1
    s.Value = t.." 秒剩余"
    wait(1)

    if plrs == 0 then
        s.Value = "存活: "..plrs
    end

    until t == 0

    s.Value = "游戏结束!"

    wait(5)

end

我期望该脚本能找到被杀死的所有玩家,但输出似乎只适用于剩余的秒数。

点赞
用户2860267
用户2860267

如果您为每个玩家的 Humanoid.Died 信号添加一个监听器,您可以跟踪每个回合哪些玩家死亡了。

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

--使用一个玩家名称的映射来跟踪活着的玩家
local playersAlive = {}
local listenerTokens = {}
local SPAWN_LOCATION = CFrame.new()

--编写一些帮助函数
local function getCountPlayersAlive()
    local numPlayersAlive = 0
    for playerName,isAlive in pairs(playersAlive) do
        numPlayersAlive = numPlayersAlive + 1
    end

    return numPlayersAlive
end

local function beginRound()
    --获取回合中所有玩家的列表
    local allPlayers = game.Players:GetChildren()
    for _,player in ipairs(allPlayers) do

        --在工作区中找到相关的玩家
        local character = game.Workspace:FindFirstChild(player.Name)
        if character then

            --为他们死亡时添加一个事件侦听器
            local humanoid = character:FindFirstChildOfClass("Humanoid")
            local deathSignalToken = humanoid.Died:Connect(function()

                --当此玩家死亡时,信号表明剩余玩家数量减少一个
                playersAlive[player.Name] = nil
            end)

            --将该玩家添加到此轮比赛的玩家列表中
            playersAlive[player.Name] = true

            --将玩家移动到游戏竞技场中
            player.Character:SetPrimaryPartCFrame(SPAWN_LOCATION)

            --在回合结束时断开此信号的连接
            table.insert(listenerTokens,deathSignalToken)

        else
            --无法找到玩家,不让他们参加此回合
        end
    end
end

local function endRound()
    --清理游戏板,重新加载玩家,执行所需操作

    --从上一轮清除任何死亡信号
    for _,token in ipairs(listenerTokens) do
        token:Disconnect()
    end
end

--设置游戏循环
local GAME_LENGTH = 30 --秒
local INTERMISSION_LENGTH = 15 --秒

spawn(function()
    while true do
        --第1阶段-设置回合
        beginRound()

        --第2阶段-进行回合比赛
        local gameTimer = GAME_LENGTH
        while gameTimer > 0 and getCountPlayersAlive() > 0 do
            local count = getCountPlayersAlive()
            print(string.format("剩余时间:%d - 剩余玩家数量:%d",gameTimer,count))
            wait(1)
            gameTimer = gameTimer - 1
        end

        --第3阶段-庆祝获胜者并清理上一轮
        endRound()

        --第4阶段-下一轮之前的过渡时间
        wait(INTERMISSION_LENGTH)
    end
end)

很抱歉这个例子太冗长了,但我希望这可以帮助您。

2019-05-22 20:24:06