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

How do you change player walkspeed based on leaderstat?

Asked by 2 years 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 2 years ago
Edited 2 years 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 2 years ago
Edited 2 years 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