首页 > 解决方案 > 根据玩家是否在区域内打开和关闭 GUI

问题描述

我有一个适用于基于回合的游戏的脚本。它从一个大厅和一个 30 秒的间歇计时器开始。然后将玩家传送到我定义的区域内的选定地图。在回合中,它会倒计时并在 180 秒后结束回合,或者如果剩下 1 名玩家。被杀死(或在 180 秒结束时仍然存在)的玩家将被传送回大厅并继续循环。我使用了下面的脚本,它运行良好。我想利用此脚本中的表格/数组来识别该区域中的玩家并关闭/使我希望仅对大厅中的玩家可见的 Spectate GUI(即不在我所在的区域中)定义包含各种圆形地图)。但我无法弄清楚如何修改这个脚本。任何帮助将不胜感激!谢谢。代码如下:

'''

while true do -- repeats forever and calls the functions

wait(2)
intermissionTimer()

chooseMap()
loadMap()
wait(2)
teleportPlayers()
wait(2)

local time = roundLength

while wait(1) do
    
    partsInRegion = workspace:FindPartsInRegion3(region, game.ServerStorage.Maps,3000) -- this returns a table 
    playersFound = {} -- table players found

    for i, part in pairs (partsInRegion) do
        if part.Parent:FindFirstChild("Humanoid") ~= nil then
            playersFound[part.Parent.Name] = part.Parent -- add players character to table
            winner = playersFound[part.Parent.Name].Name
            print (winner)
            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
    

    if time == 0 then
        Status.Value = "Round over!"
        workspace.SoundLibrary.Nowinner:Play()
        break
    elseif Length(playersFound)== 1 then
        
        workspace.SoundLibrary.Winner:Play()
        Status.Value = winner.. " wins!"

        wait(5)
        break
    else
        Status.Value = time .." seconds left"
        time = time - 1
    end 
end 

wait (2)
teleportBack()  
deleteMap()

结尾

'''

标签: roblox

解决方案


在你的 for 循环结束时,你有一张桌子,里面塞满了仍在游戏中的玩家。您可以使用Players 服务获取游戏中所有玩家的列表。每个玩家对象都作为孩子存储在那里,您可以将幸存玩家的名字与所有玩家的名字进行比较,以找出谁死了。

local partsInRegion = workspace:FindPartsInRegion3(region, game.ServerStorage.Maps,3000)
local playersFound = {}

-- find all the players still alive
for k, part in pairs(partsInRegion) do
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        playersFound[part.Parent.Name] = part.Parent
    end 
end

-- figure out who is dead
local allPlayers = game.Players:GetChildren()
for i, player in ipairs(allPlayers) do
    -- if they are not still in the game, they must be dead
    if playersFound[player.Name] == nil then
        -- get the Spectate gui, replace this line with your actual gui
        local gui = ReplicatedStorage.SpectateGui:Clone()

        -- give the dead player the spectate gui, if they don't already have it
        if player.PlayerGui[gui.Name] == nil then
            gui.Parent = player.PlayerGui
        end
    end
end

推荐阅读