Ad
Log in to vote
0

How do you change player walkspeed based on leaderstat?

Asked by 11 days ago

For example if a players leaderstat equals to 10, the player will have 10 walkspeed.

2 answers

Log in to vote
0
Answered by 11 days ago
Edited 11 days ago

localscript

while wait(0.1) do
  game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = game.Players.LocalPlayer.leaderstats.Speed.Value
end
Ad
Log in to vote
0
Answered by 10 days ago
Edited 10 days ago

Use the event :GetPropertyChangedSignal() in the walkspeed leaderstat and it will fire if a certain property changes.

For example, we put "Name" between the parenthesis. If the Name of the walkspeed leaderstats changes, this will fire. For this one, we must use "Value".

Also don't forget to do the same thing to the humanoid walkspeed.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player) -- this event will fire when a player joins
    local Leaderstats = Instance.new("Folder", Player) -- creates the leaderstats inside the Player
    Leaderstats.Name = "leaderstats"

    local WalkSpeed = Instance.new("NumberValue", Leaderstats)
    WalkSpeed.Name = "Speed"

    Player.CharacterAdded:Connect(function(Character) -- this event will fire when the player's character has loaded/respawned
        local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
        WalkSpeed.Value = Humanoid.WalkSpeed

        WalkSpeed:GetPropertyChangedSignal("Value"):Connect(function() -- this event will fire if the Value of WalkSpeed changes
            Humanoid.WalkSpeed = WalkSpeed.Value
        end)

        Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function() -- this event will fire if the speed of player changes
            WalkSpeed.Value = Humanoid.WalkSpeed
        end)
    end)
end)

Answer this question