Script not working after Death

So, I’m trying to make it so if my endurance stat changes ( Which is in Player.Parent ) it
would change my max Health. The script works until I die for some reason.

game.Players.PlayerAdded:Connect(function(plr)
local Endurance = plr:WaitForChild(“Stats”).Endurance.Value
local Bonus = Endurance * 10
plr.Character:WaitForChild(“Humanoid”).MaxHealth = plr.Character:WaitForChild(“Humanoid”).MaxHealth + Bonus
end)

If I do this in CharacterAdded it works but then It’ll fire multiple times the more times I die since more characters have been added.

*** EDIT sorry for the confusion I forgot to say that this is a Changed function so …

game.Players.PlayerAdded:Connect(function(plr)
plr:WaitForChild(“Stats”).Endurance.Changed:Connect(Function(NewEnd)
local Bonus = NewEnd* 10
plr.Character:WaitForChild(“Humanoid”).MaxHealth = plr.Character:WaitForChild(“Humanoid”).MaxHealth + Bonus
end
end)

Have your tried using the script but in a Player.CharacterAdded function rather than a Players.PlayerAdded one ?

1 Like

I beg your pardon, could you explain it a bit elaborately?

Edit : Did you mean that once you die it fires the event multiple times?
If that is the case then try using a debounce variable…

1 Like

Yes, but this script is waiting for a value to be changed, If I die in the game then Plr.Character will be fired again and the script will run twice which will cause more lag if it fires lets say 20 times because you died 20 times

When I die, And the values change after I die it does not fire the Changed: function

So you mean that the endurance does not increase after you die?
(I mean that it does change but is not detected by the server)

1 Like

From what I can see, correct me if I am wrong, the script has no reason to keep functioning. Once the player is added, you define a couple values and wait until the health conducts itself. Try making a separate function inside of that using Humanoid.Died

Would look something like

game.Players.PlayerAdded:Connect(function(player)
  player.CharacterAdded:Connect(function(character)
    --code goes here
    character:WaitForChild("Humanoid").died:Connect(function(died)
      print("Character has died")
    end)
  end)
end)

I believe this should make the code reset depending on what you put, you can also use a function for that process, like this

function updatestats()
  --code you want to work goes here
end
game.Players.PlayerAdded:Connect(function(player)
  updatestats()
  player.CharacterAdded:Connect(function(character)
    --code goes here
    character:WaitForChild("Humanoid").died:Connect(function(died)
      updatestats()
    end)
  end)
end)

Let me know if this works!

2 Likes

I don’t know if this works but, I usually do yield for the character to load and then execute the code

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Wait()
    -- do the other code
end)

Edit: Another solution might be doing a separate server script, where you just type this :

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
       -- do the code for the character
    end)
end)
1 Like

Consider using StarterPlayerScripts if you need stuff to work on Characters, these scripts automatically place themselves inside the character model when the character spawns into the Workspace.

If there is a usecase where you need a server script, consider using this code

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(function(chr)
      --do stuff with a new character
   end
end)

It’s worth noting that CharacterAdded fires before the character has fully loaded, so make sure to use WaitForChild()

1 Like

Yes, The int Value is changed but the script no longer detects the change after death

I am really sorry for keep asking questions back to you…
but just so that I can understand your exact problem, are you changing the value of endurance after you die?
And if so then are you changing it in an another script?

1 Like

Yes

So basically

this is function is active when a plr is added

    Game.Players.PlayerAdded:Connect(function(plr)

        plr:WaitForChild("Stats").Endurance.Changed:Connect(function(NewValue)
        bonusHealth = NewValue * 5
        plr.Character:WaitForChild("Humanoid").MaxHealth =   plr.Character:WaitForChild("Humanoid").MaxHealth + bonusHealth 
end
end)

I change the Value of Endurance in another script, this one only checks if the value is changed
and It does not detect the change after the player dies

The premise of the issue I previously said would still work with the changed event, just append it.


function update(plr)
  local Bonus = NewEnd* 10
  plr.Character:WaitForChild(“Humanoid”).MaxHealth = 
  plr.Character:WaitForChild(“Humanoid”).MaxHealth + Bonus
 
end
game.Players.PlayerAdded:Connect(function(plr)
  plr.CharacterAdded:Connect(function(character)
    plr:WaitForChild(“Stats”).Endurance.Changed:Connect(Function(NewEnd)
      update(plr)
    end)
  character:WaitForChild("Humanoid").died:Connect(function(died)
    plr:WaitForChild(“Stats”).Endurance.Changed:Connect(Function(NewEnd)
      update(plr)
    end)
    end)
  end)
  end)
end)

Dont quote me for the ends! I did not track them ://

2 Likes

Lol the amounts of ends Xd, well thanks this will probs work :>