roblox - 根据玩家是否在区域内打开和关闭 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()
结尾
'''
解决方案
在你的 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
推荐阅读
- javascript - 灯廊更新索引
- haskell - 野外拉链
- android - 将 BT 置于可发现模式并在 Xamarin Droid 中接受 SPP 连接
- angular - 用于嵌套 json 数组的 Angular 4 管道过滤器
- c# - TPL DataFlow 未处理所有消息
- c# - 使用不同的 propertyValue 类型反序列化 json
- servicestack - ServiceStack Razor 找不到正确的视图
- c# - Swagger 页面是 404 Not Found in MVC net-core
- javascript - Angular/Regex 输入框中的空格问题?我需要限制第一个空格,然后允许用户在第一个字符后输入空格
- c - 找出套接字的传输类型