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?
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.
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
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.
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
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