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

Whats wrong with my script its supposed to add 5 health and .5 walk speed every time you level up?

Asked by 4 years ago

It use to work but now it doesn't

game.Players.PlayerAdded:Connect(function(Player)                  
    Player.CharacterAdded:Connect(function(Character)              
        local Stats            = Player:WaitForChild("leaderstats")
        local Level            = Stats:WaitForChild("Level")      
        local Humanoid         = Character:WaitForChild("Humanoid")

        Humanoid.MaxHealth     = (Level.Value*5)+100             
        Humanoid.Health        = Humanoid.MaxHealth                

        Humanoid.WalkSpeed     = (Level.Value*.5)+16              
        Humanoid.Health        = Humanoid.MaxHealth
    end)                                                           
end)                                             
0
You haven't given us much to work with... Is there anything indicating what the problem might be? Marty999 82 — 4y
0
it doesnt increase your speed or your health AlphaFlame5 -1 — 4y
0
Also, you say the health and walk speed should be changed when a player levels up, but it seems there is nothing in the script to indicate this is actually being done Marty999 82 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

As your code is currently, both the speed and health of your character is scaled according to your level every time Player.CharacterAdded is fired, which means that it changes only when you spawn into the game or respawn after you die.

In order to make your character change whenever you level up, you must attach some kind of event to the changing of your player's level stat, such as the :GetPropertyChangedSignal method, which I will use in the updated version of the code:

game.Players.PlayerAdded:Connect(function(Player)  
    Player.CharacterAdded:Connect(function(Character)              
        local Stats            = Player:WaitForChild("leaderstats")
        local Level            = Stats:WaitForChild("Level")      
        local Humanoid         = Character:WaitForChild("Humanoid")

        -- Leave this here so health and walkspeed is set when your character loads
        Humanoid.MaxHealth     = (Level.Value*5)+100            
        Humanoid.Health        = Humanoid.MaxHealth
        Humanoid.WalkSpeed     = (Level.Value*.5)+16  

        Level:GetPropertyChangedSignal("Value"):Connect(function() -- Fires whenever a change is made to the value of the level stat

            -- Change health and walkspeed every time your level changes
            Humanoid.MaxHealth     = (Level.Value*5)+100             
            Humanoid.Health        = Humanoid.MaxHealth
            Humanoid.WalkSpeed     = (Level.Value*.5)+16  
        end)
    end)                                                           
end)      
0
Thank You! This really helped me. AlphaFlame5 -1 — 4y
Ad

Answer this question