Why this script dosent work?

it just needed to detect if the player it died or not

a script in startercharacterscripts will automatically add a script in the character meaning the character died/joined (will ignore if character joined)

OP create a new topic about this so new people can help. This thread already have 40+ replies. Or whatever

at this line

					PlayerTouched[character.Name] = nil -- Remove player from the list when they die

try

					table.remove(PlayerTouched,table.find(PlayerTouched,player))

What’s p? Also it’s already in my solution, but it doesn’t work

1 Like

oh i think his script need to be managed in a different way

Tell him how, Because I don’t know how.

1 Like

well i dont either soooo

> Blockquote

Been here for 2 hours lmao, Imma head out

1 Like

im so dead at this thing bruh idk why it doesn’t work :sob:

1 Like

please, can you show the explorer of the workspace

image

But this is indeed the better way to do it.

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")

local valueObject = replicatedStorage:FindFirstChild("SpawnedIn")
local spawnedIn = {}

local function humanoidDied(character)
	local position = table.find(spawnedIn, character.Name)
	if position then
		table.remove(spawnedIn, position)
	end
	valueObject.Value = table.maxn(spawnedIn)
end

local function characterAdded(character)
	local humanoid = character:FindFirstChild("Humanoid")
	local function died()
		humanoidDied(character)
	end
	table.insert(spawnedIn, character.Name)
	valueObject.Value = table.maxn(spawnedIn)
	humanoid.Died:Connect(died)
end

local function playerAdded(player)
	player.CharacterAdded:Connect(characterAdded)
end

local function playerRemoved(player)
	local position = table.find(spawnedIn, player.Name)
	if position then
		table.remove(spawnedIn, player.Name)
	end
	valueObject.Value = table.maxn(spawnedIn)
end

players.PlayerRemoving:Connect(playerRemoved)
players.PlayerAdded:Connect(playerAdded)

afbeelding

Tested it and it works.

I highly reccomend adding that value to replicated storage so other code can read that value.

It looks good, but maybe there is a reason why OP uses spawn objects instead of player events. or I don’t know :stuck_out_tongue:

im gonna try this one soon, i am trying some suggestions in here

local Players = game:GetService("Players")
local PlayerNumber = script.Parent:FindFirstChild("PlayerNumber")
local debounce = false
local Spawns = script.Parent:FindFirstChild("Spawns"):GetChildren()
local PlayerTouched = {}

for _, spawnLocation in ipairs(Spawns) do
	if spawnLocation.Name ~= "PlayerNumber" and not spawnLocation:IsA("Script") and not spawnLocation:IsA("LocalScript") then
		spawnLocation.Touched:Connect(function(otherPart:Part)
			local character = otherPart.Parent
			if not Players:FindFirstChild(character.Name) then
				return
			end
			if character and not table.find(PlayerTouched,character.Name) and debounce == false then
				debounce = true
				table.insert(PlayerTouched,character.Name)
				print(table.unpack(PlayerTouched))
				PlayerNumber.Value = PlayerNumber.Value + 1
				local humanoid = character:WaitForChild("Humanoid")
				if humanoid then
					humanoid.Died:Connect(function()
						PlayerNumber.Value = PlayerNumber.Value - 1
						if table.find(PlayerTouched,character.Name) then
							table.remove(PlayerTouched,table.find(PlayerTouched,character.Name)) -- Remove player from the list when they die
						end
						print("died")
						print(table.unpack(PlayerTouched))
					end)
				end
				task.wait(Players.RespawnTime + 0.5) -- Prevents the player from touching the spawn when they are dead
				debounce = false
			end
		end)
	end
end

Hopefully this might work.

The problem with doing this with a touched event is simple, alot of things COULD go wrong and you are just making long code.

You can simply replicate this by detecting if the player spawns or not.

Another issue with the touched event is that there are things that could go wrong,
for example the player not touching it or it not registering.

Also, use task.wait() it’s more reliable

Thanks for the feedback! I am going to edit the post to use task.wait now.

1 Like