Why this script dosent work?

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?

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

Makes sense, but what if op place the script on workspace as well?

still it didn’t work, and i still don’t see and error

Where do you put the spawn objects and the script?

i put this script in the workspace

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

Guys, this is where the problem are.

Can you explain what is the reason?

Well, first of all, did it work?