I’ve been searching for a more optimal alternative to my script (detects player alive on roundstart);
In short, it uses a while loop to check players using getpartsinpart. I didn’t use humanoid states because there are chances that player can get flinged or go outside the map.
But in some rare occasions, getpartsinpart just doesn’t detect the player at all or detects the player without stopping, making the round not able to stop after <2 remaining players.
function Round:Start(mapInstance : Instance)
self.Status = "Round"
updateStatus(self.Status)
for index = self.RoundTime, 0, -1 do
self.Timer = index
updateTimer(self.Timer)
--// WinCondition
local playersAlive = {}
local alreadyChecked = {}
local aliveLookup = {}
local RoundBox = mapInstance:WaitForChild("RoundBox")
if RoundBox then
local partsInside = workspace:GetPartsInPart(RoundBox)
for _, part in ipairs(partsInside) do
local character = part:FindFirstAncestorOfClass("Model")
local player = character and Players:GetPlayerFromCharacter(character)
local humanoid = character and character:FindFirstChildOfClass("Humanoid")
if player and not alreadyChecked[player] then
if humanoid and humanoid.Health > 0 then
table.insert(playersAlive, player)
alreadyChecked[player] = true
player:AddTag("inRound")
end
end
end
end
for _, player in ipairs(playersAlive) do
aliveLookup[player] = true
end
for player, _ in pairs(self.RoundStats) do
if aliveLookup[player] then
self.RoundStats[player] += 1
end
end
Events.PlayersAlive:FireAllClients(playersAlive)
if #playersAlive == 1 then
warn("end round, 1 player left")
break
end
task.wait(1)
end
--// Show win announcement
local results = {}
for player, roundtime in pairs(self.RoundStats) do
table.insert(results, {
Player = player,
Time = roundtime
})
end
table.sort(results, function(a, b)
return a.Time > b.Time
end)
--// SEND THE RESULTS
Events.RoundAnnouncement:FireAllClients(results)
Events.ToggleSpectate:FireAllClients(false)
self.IsRound = false
lobbyTeleport(self)
for i, data in ipairs(results) do
Manager.AdjustCoins(data.Player, math.round((8/i)) * #results)
if i == 1 then Manager.AdjustWins(data.Player, 1)
end
end
--// Reset back the roundstats
self.Participants = {}
self.RoundStats = {}
end
Should I look for a different approach or use a better alternative of getpartsinpart?
Thanks in advance!
Personally, I would just make it so that every player that you want to enter into the game is put into a table and whenever they die they’re removed from the table. Then whenever you need the alive players, just return the table.
It’s a lot simpler and doesn’t require making a whole hitbox for it.
Well then I’m not too sure what else you could do, but I would also probably just do the table idea and keep track of each player’s position and if they’re outside of certain bounds then you remove them from the table.
In cases where .Touched may be unreliable, another alternative solution is to create a non-collidable “border” that covers the whole map, where for a specific rate (like every few seconds), you can run workspace:GetPartBoundsInBox() and check if there are any instances that aren’t in the map:
local border: BasePart = workspace.PrimaryPart
local currentPlayers: {string} = {} -- assuming this is a table of character names
-- on round start!
while true do
local playersWithinMap = {}
-- check all players within border
for _,part in workspace:GetPartBoundsInBox(border.CFrame, border.Size) do
local character: Model = part:FindFirstAncestorWhichIsA("Model")
local humanoid: Humanoid = character and part:FindFirstChildWhichIsA("Humanoid")
if humanoid then
if table.find(currentPlayers, character.Name) then
table.insert(playersWithinMap, character.Name)
end
end
end
-- compare if currentPlayers array has any players that arent in border, if yes, remove them
for _,currentPlayerName in currentPlayers do
local index = table.find(playersWithinMap, currentPlayerName)
if not index then
table.remove(playersWithinMap, index)
end
end
task.wait(5) -- example rate
end
A bit more complicated but it would reduce the possibility of .Touched breaking the game because it failed to trigger.
You could just use this and modify your original code to use GetPartsBoundsInBox() instead of your original method. It’d technically work the same way and you wouldn’t need 4-6 hitboxes.