What i'm trying to do is make a script that reads the leaderstat "Health" and when it changes it updates the maxhealth of that player. (It also needs to be able to make it so they keep the max health even after death.) I am also going to be looking into the data store to save the IntValue and load it when a player leaves/joins.
I have been looking around the wiki and I really haven't had any luck.
I was trying to do a localscript that's placed within the StarerGUI... Here is the script..
stat = "Health" game.Players.PlayerAdded:connect(function(player) player.CharacterAdded:connect(function(character) local p = game.Players.LocalPlayer character.Humanoid.MaxHealth = p.leaderstats:FindFirstChild(stat) end) end)
What it currently does is only gets the stat, I added in a few prints and it only got the stat = "Hleath"
Well, the script you have will detect NEW players coming in, but not your player.
Try this:
player = game.Players.LocalPlayer -- Your player char = player.Character stat = player.leaderstats.Health char.Humanoid.MaxHealth = stat.Value -- This will load it every time your character resets stat.Changed:connect(function() -- Triggers when (stat) changes. char.Humanoid.MaxHealth = stat.Value end)
I wouldn't use a LocalScript
, I'd put a Script
in ServerScriptService
with the following code-
local Players = Game:GetService("Players") Players.PlayerAdded:connect(function(plr) -- when a player joins local health = plr:WaitForChild("leaderstats"):WaitForChild("Health") -- wait for leaderstat 'Health' to load plr.CharacterAdded:connect(function(char) -- when that player respawns char:WaitForChild("Humanoid").MaxHealth = health.Value -- set the max health to their leaderstat 'Health' value end) health.Changed:connect(function(h) -- when their leaderstat 'Health' changes if plr.Character then -- if the character exists at that moment plr.Character:FindFirstChild("Humanoid").MaxHealth = h -- set the humanoid's health to the leaderstat 'Health' end end) end)