Roblox 游戏脚本检测“最后幸存者”胜者

我正在开发一个有 6 个玩家的基于回合的 Roblox 游戏,每个回合的结束条件是时间用完或仅剩一个幸存者。每个回合结束后,玩家(以及在回合中死亡的玩家)都会被传送回大厅,随后进行简短的休息,然后再次传送回地图开始新的回合(会随机选择不同的地图)。我的游戏策略是创建一个包围所有地图的大区域,并在倒计时的过程中不断检测区域中的玩家人数。当人数减少到 1 时,回合就会结束,并宣布胜者。这种方法有两个问题:

  1. 我不得不创建一个巨大的区域来包含所有不同的地图,随着我添加更多的地图,这个区域会越来越大。当最多只有 6 个玩家在一个地图中时,在如此大的区域中检测玩家人数是否可行?

  2. 我的玩家检测脚本不起作用。它只能检测到一个玩家,但是当有 2 个玩家时,它仍然返回“1”作为玩家数量。为了解决这个问题,我创建了一个简单的游戏,检测玩家是否在区域(灰色基板)内以及当玩家离开区域(红色基板)时。当我在公开场合上运行该游戏时,即在其中有 2 个玩家时,它只显示 1 个玩家在游戏中(我将其显示在左上角 GUI 中)。我的代码如下——我认为问题出在 playersFound 表/字典是如何工作的上。

代码:

regionPart = game.Workspace.RegionFromThisPart -- 我创建了一个透明的立方体来定义区域
pos1, pos2 = (regionPart.Position - (regionPart.Size/2)),(regionPart.Position + (regionPart.Size/2))
region = Region3.new(pos1,pos2)
Status = game.ReplicatedStorage.Status

wait(5)

while wait(1) do

    partsInRegion = workspace:FindPartsInRegion3(region, game.Workspace.Baseplate,1000)
    playersFound = {} -- 玩家发现的表

    for i, part in pairs (partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            playersFound["playerinregion"] = part.Parent -- 将玩家角色添加到表中
            print (i, part) -- 0
        end
    end

    function Length(playersFound)
        local counter = 0
        for _, v in pairs(playersFound) do
            counter =counter + 1
        end
        return counter
    end

    Status.Value = Length(playersFound) .." players in region"

end

奖励问题:我创建的用于显示区域内玩家数量的 GUI 在我的屏幕上显示,但不会在其他玩家的屏幕上显示(他们的屏幕上显示“标签”)。

我已经浪费了大量的时间,非常需要帮助解决这个问题。

谢谢!

点赞
用户2860267
用户2860267

你绝对是对的,你的问题源于你如何将玩家放入 playersFound 表中。因为你每次在表中使用相同的关键字,所以你正在覆盖旧的玩家,而新发现的任何玩家都会被覆盖。 相反,尝试使用唯一关键字保存玩家。这可以是 i 或更好的是玩家的名字。既然你只是在循环遍历表,为什么不只保留有多少个玩家的计数器。

local regionPart = game.Workspace.RegionFromThisPart -- I created a transparent cube to define the region
local pos1 = (regionPart.Position - (regionPart.Size/2))
local pos2 = (regionPart.Position + (regionPart.Size/2))
local region = Region3.new(pos1,pos2)
local Status = game.ReplicatedStorage.Status
local BasePlate = game.Workspace.Baseplate
local time = 30 -- seconds

wait(5)

while wait(1) do

    -- count how many players are left
    local playersFound = {}
    local totalPlayersFound = 0
    local partsInRegion = workspace:FindPartsInRegion3(region, baseplate, 1000)
    for i, part in pairs(partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            -- keep track of all the players still alive
            -- since a player's character is made of many parts, each player will show up multiple times,
            -- so only hold onto each player once
            local character = part.Parent
            if (playersFound[character.Name] == nil) then
                playersFound[character.Name] = character
                print ("Found Player : ", character.Name)
                totalPlayersFound = totalPlayersFound + 1
            end
        end
    end

    -- 根据游戏状态更新UI
    if time == 0 then
        Status.Value = "回合结束!"
        break
    elseif totalPlayersFound == 1 then
        -- since we know that there's only one entry in the playersFound table,
        -- grab it using the next() function
        local playerName, character = next(playersFound)
        StatusValue = "赢家 : " .. playerName
        wait(5)
        break
    --elseif totalPlayersFound  == 0 then
        -- make sure to check in case no players are left
    else
        Status.Value = tostring(totalPlayersFound) .. " 位玩家在本回合中"
        time = time - 1
        -- DEBUG :
        -- print("There are " .. tostring(totalPlayersFound) .. " players in the round")
        -- for playerName, character in pairs(playersFound) do
        --     print(" - " .. playerName .. " is still in the game")
        -- end
    end
end

至于你的奖励问题,你应该问一个后续问题并发布代码来显示你正在如何创建标签并更新它。

2020-12-14 21:35:13