i have this script that when a player touches a Spawnlocation it puts the players name in a variable
and when the player dies it will get removed
but it doesn’t work for some reason
here’s the script
local Players = game:GetService("Players")
local PlayerNumber = script.Parent:FindFirstChild("PlayerNumber")
local Spawns = script.Parent:FindFirstChild("Spawns"):GetChildren()
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
In Roblox Studio, go to View → Click on Output. A console window should show at the bottom of the program. Are there any errors or logs it reaches? My guess is maybe an object isn’t being found or doesn’t exist.
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