Why this script dont work when im dead

This script dont work even i reset.

  • Local Script
local Player = game.Players.LocalPlayer
local Start = 0 -- If player is dead back to 0


game.Players.PlayerAdded:Connect(function(Player)

	Player.CharacterAdded:Connect(function(char)

		local hum = char:WaitForChild("Humanoid")	

		hum.Died:Connect(function()
			Start = 0
		end)
	end)
end)

while wait(0.5) do
		
	if Player:WaitForChild("HeadStats").Golden.Value == 1 and Start == 0 then -- proceed here
		
	
		Start = 1 -- Change to 1
		
		game.ReplicatedStorage.TrailScripts.Equip.Golden:FireServer()
	
		
			
	end
	end

Or can someone help me to change the value of start and make it to 0 after they die?

where is the local script location in explorer ?

Its located in or on starter gui

Also as i know scripts dose not work in replicated storage

As far as I know, the PlayerAdded event doesn’t work LocalScripts, plus you won’t even need to use the PlayerAdded event if it’s parented to something like say, StarterCharacterScripts where the scripts always runs when the player is added in the game. Since the PlayerAdded event was never called. The died event is also not being called.

Edit: The script also works when it’s placed in StarterGui. After spawning.

1 Like

As you know the local script will reset whenever you die ( except while in StarterPlayerScripts )
So you don’t need to use PlayerAdded and CharacterAdded function, try to follow this

local Player = game.Players.LocalPlayer
local Start = 0

repeat wait() until Player.Character:FindFirstChild("Humanoid")
local Humanoid = Player.Character:FindFirstChild("Humanoid")

Humanoid.Died:Connect(function()
   Start = 0
end)

--Your script will continue here

Still dont fix the problem. mhm

1 Like

You shouldn’t’ use the repeat until method, it’s Inefficient. Instead just use the CharacterAdded event.

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local Start = 0

local Humanoid = char:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
   Start = 0
end)

--Your script will continue here

The reason you should not use repeat until is because it eats alot of data, and can slow down a Low End Computer.

More info - Busy waiting means a process simply spins (does nothing but continue to test its entry condition) while it is waiting to enter its critical section. This continues to use (waste) CPU cycles, which is inefficient.

1 Like

Try this code…

local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local Start = 0

local Humanoid = char:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
   Start = 0
end)

--Your script will continue here
1 Like

What does the while loop do ? I need to know it, at the beginning your Start = 0, the while loop made Start = 1 when Player got spawned, when player died the start = 0 but the while loop was still running , it would set Start to 1 after player died, you better use break after set the Start to 1

while wait(0.5) do
	
if Player:WaitForChild("HeadStats").Golden.Value == 1 and Start == 0 then -- proceed here
	

	Start = 1 
	
	game.ReplicatedStorage.TrailScripts.Equip.Golden:FireServer()

	break
		
end
end

It wont work, That only work when im joining. But when i die nope.

1 Like