Can’t you use the playeradded event or put a script in starter character scripts since it runs every time the character respawn? This should also work if the leave too
it does work, the only problem is that when the player dies it doesn’t get removed in a variable
this part specifically
humanoid.Died:Connect(function()
PlayerCountModule[character.Name] = nil -- Remove player from the list when they die
PlayerNumber.Value = PlayerNumber.Value - 1
print("ded")
end)
I’ve rewritten the the table structure with table module, optimise it too.
for _, spawnLocation in ipairs(Spawns) do
spawnLocation.Touched:Connect(function(otherPart)
local character = Players:GetPlayerFromCharacter(otherPart.Parent)
if character and not table.find(PlayerTouched, character.Name) then
table.insert(PlayerTouched, character.Name)
PlayerNumber.Value = PlayerNumber.Value + 1
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
table.remove(PlayerTouched, table.find(PlayerTouched, character.Name)) -- Remove player from the list when they die
PlayerNumber.Value = PlayerNumber.Value - 1
print("ded")
end)
end
end
end)
end
The problem is that the spawn objects are in the script. they have to be in the workspace to be able to be touched. Make them anchored on. Uncancollidable, cantouch and canquery off. Make the spawns transparent and then place them in the workspace.
local Players = game:GetService("Players")
local PlayerNumber = script.Parent:FindFirstChild("PlayerNumber")
local Spawns = game.Workspace:FindFirstChild("Spawns")
local PlayerTouched = {}
for _, spawnLocation in ipairs(Spawns) do
spawnLocation.Touched:Connect(function(otherPart)
local character = Players:GetPlayerFromCharacter(otherPart.Parent)
if character and not PlayerTouched[character.Name] then
PlayerTouched[character.Name] = character.Name
PlayerNumber.Value = PlayerNumber.Value + 1
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
PlayerTouched[character.Name] = nil -- Remove player from the list when they die
PlayerNumber.Value = PlayerNumber.Value - 1
print("ded")
end)
end
end
end)
end
this is the reason it doesnt work. consider changing it to this
local Players = game:GetService("Players")
local PlayerNumber = script.Parent:FindFirstChild("PlayerNumber")
local Spawns = game.Workspace:FindFirstChild("Spawns")
local PlayerTouched = {}
for _, spawnLocation in Spawns:GetChildren() do
spawnLocation.Touched:Connect(function(otherPart)
local character = Players:GetPlayerFromCharacter(otherPart.Parent)
if character and not table.find(PlayerTouched, character.Name) then
table.insert(PlayerTouched, character.Name)
PlayerNumber.Value = #PlayerTouched
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
table.remove(PlayerTouched, table.find(PlayerTouched, character.Name)) -- Remove player from the list when they die
PlayerNumber.Value = #PlayerTouched
print("ded")
end)
end
end
end)
end