Why this script dosent work?

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

3 Likes

try replacing all your FindFirstChild with WaitForChild

3 Likes

i replace all of it, it didn’t work

1 Like

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.

2 Likes

What part specifically that doesn’t work? Any error in the console?

1 Like

Does “ded” printed? Try changing ipairs() with pairs().

1 Like

the thing is is that theres no errors in the output

1 Like

i dont see the error on it but it dosent work

1 Like

What do you mean by doesn’t work? Is it PlayerTouched table is still empty and PlayerNumber doesn’t change even though it’s touched?

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

https://devforum.roblox.com/t/characteradded-not-firing-when-the-player-respawns/587532/1

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)

this part doesn’t seem to work

1 Like

Maybe put it out of the forloop scope?

Use table.removed it should allow you to remove the player

1 Like

ive never heard of table.removed before, whats that?

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
2 Likes

You should read documentation while developing.

Does this resolved your problems op?

Why do you need so much code when all you need to do is use characteradded and humanoid.died in starterplayer scripts?

Don’t reply to me, reply to the op.

Why are you not responding op?