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
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)
This is the original code by op, LinuxKat code is modified and it is wrong as you said.
local Players = game:GetService("Players")
local PlayerNumber = script.Parent:WaitForChild("PlayerNumber", 30)
local Spawns = script.Parent:WaitForChild("Spawns", 30):GetChildren()
local PlayerTouched = {}
for _, spawnLocation in Spawns do
spawnLocation.Touched:Connect(function(hit)
local character = Players:GetPlayerFromCharacter(hit.Parent)
if character and not table.find(PlayerTouched, character.Name) then
table.insert(PlayerTouched, character.Name)
PlayerNumber.Value = #PlayerTouched
local humanoid = character:FindFirstChildWhichIsA("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)