首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Roblox游戏脚本检测“唯一幸存者”获胜者

Roblox游戏脚本检测“唯一幸存者”获胜者
EN

Stack Overflow用户
提问于 2020-12-14 19:06:06
回答 1查看 511关注 0票数 0

我正在进行一场6回合的基于罗布洛的比赛,如果任何一轮的时间都结束了,或者每一名球员,除了一名幸存者,都会结束比赛。在每轮比赛结束后(以及在比赛中被杀的球员)被传送回大厅,然后短暂休息,然后传送到地图中开始新的一轮(随机选择了很多不同的地图)。我这个游戏的策略是在所有地图周围创建一个很大的区域,在计时器倒计时的时候,不断地检测该区域的#玩家。当这个数字达到1时,这一轮将结束,并将宣布一名胜利者。这种方法有两个问题:

  1. ,我必须创建一个巨大的区域来包含所有不同的地图,当我添加更多的地图时,这个区域将变得越来越大。是否在这么大的区域内检测到#球员,而一次最多只有6个玩家在一个地图上?

  1. 我的播放器检测脚本不起作用。它检测到一个玩家,但是当有2个玩家时,它仍然返回"1“作为玩家的#。为了解决这个问题,我创建了一个简单的游戏,用来检测玩家何时在某个区域(灰色基板)和何时离开该区域(红色基板)。当我在游戏中与2名玩家一起公开运行游戏- https://www.roblox.com/games/6060804433/Rounds时,它只返回游戏中的1名玩家(我在左上角图形用户界面中显示了这一点)。我的代码如下-我认为问题在于表/字典playersFound是如何工作的。

代码:

代码语言:javascript
复制
regionPart = game.Workspace.RegionFromThisPart -- I created a transparent cube to define the region
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 = {} -- table of players found

    for i, part in pairs (partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            playersFound["playerinregion"] = part.Parent -- add players character to table
            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显示了我屏幕上的玩家数量,但没有显示其他玩家的屏幕(屏幕上显示的是“标签”)。

我花了难以置信的时间旋转我的车轮,并将非常感谢帮助解决这个问题。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-12-14 21:35:13

你是完全正确的,你的问题来源于你如何把球员放到你的playersFound表中。因为每次都在表中使用相同的键,所以您正在用找到的任何新键覆盖老玩家。相反,尝试使用一个独特的键来保持球员。这可能是i,或者更好的,玩家的名字。既然你又要在桌子上转圈了,为什么不保持一个计数器,看看有多少名球员。

代码语言:javascript
复制
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

    -- EDIT BASED ON COMMENTS --
    -- update the UI based on the game state
    if time == 0 then
        Status.Value = "Round over!"
        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 = "Winner : " .. playerName
        wait(5)
        break
    --elseif totalPlayersFound  == 0 then
        -- make sure to check in case no players are left
    else
        Status.Value = tostring(totalPlayersFound) .. " players in round"
        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

至于你的奖金问题,你应该问一个后续问题,并张贴代码,显示你是如何创建标签和更新它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65294983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档