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)
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
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)
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()
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?
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