Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

MaxHealth Changer based on IntValue?

Asked by
lomo0987 250 Moderation Voter
10 years ago

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"

2 answers

Log in to vote
0
Answered by
hiccup111 231 Moderation Voter
10 years ago

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)

Ad
Log in to vote
0
Answered by
Ekkoh 635 Moderation Voter
10 years ago

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)
0
Nope, it still doesn't seem to work correctly. :/ lomo0987 250 — 10y

Answer this question