我正在进行一场6回合的基于罗布洛的比赛,如果任何一轮的时间都结束了,或者每一名球员,除了一名幸存者,都会结束比赛。在每轮比赛结束后(以及在比赛中被杀的球员)被传送回大厅,然后短暂休息,然后传送到地图中开始新的一轮(随机选择了很多不同的地图)。我这个游戏的策略是在所有地图周围创建一个很大的区域,在计时器倒计时的时候,不断地检测该区域的#玩家。当这个数字达到1时,这一轮将结束,并将宣布一名胜利者。这种方法有两个问题:
,
代码:
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显示了我屏幕上的玩家数量,但没有显示其他玩家的屏幕(屏幕上显示的是“标签”)。
我花了难以置信的时间旋转我的车轮,并将非常感谢帮助解决这个问题。
谢谢!
发布于 2020-12-14 21:35:13
你是完全正确的,你的问题来源于你如何把球员放到你的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
-- 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至于你的奖金问题,你应该问一个后续问题,并张贴代码,显示你是如何创建标签和更新它。
https://stackoverflow.com/questions/65294983
复制相似问题