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

How to set set a persons walkspeed?

Asked by
wjs3456 90
9 years ago

Ok so what I am attempting to do is make it so that if you are on the orange team it will make your walkspeed higher than usual. I tried adding a loop but it doesn't seem to continuously make that your walkspeed once you die.

So basically I would like to know - How can I make the players walkspeed continuously higher than usual?

Script:

while wait() do
                for i,v in pairs(game.Players:GetPlayers()) do
                    if v.TeamColor == BrickColor.new("Bright orange")then
                    v.Character.Humanoid.WalkSpeed = 30
                end
            end
        end

Thanks

1 answer

Log in to vote
0
Answered by 9 years ago

First off, you want to ensure that v.Character is accessible, otherwise it'll throw a null member error. When your character dies, trying to access the player's character can return an error as the player is being deleted and respawned into the game.

Something I like to do is use WaitForChild() on the player's name. Your style would work fine until that player died, and then it'd eventually most likely error. In this example, I'll connect a player added event listener and a Character Added event to the newly joining player, to ensure that every time the player loads, his walkspeed will be set accordingly.

Or, on a local script version, I'll simply connect the PlayerAdded event to the local Player.

-- Local Script version
wait(); -- to help ensure it loads
local Player = game:GetService("Players").LocalPlayer; 
Player.CharacterAdded:connect(function()
    local character = Workspace:WaitForChild(Player.Name);
    if not character:FindFirstChild("Humanoid") then
        return;
    end
    character.Humanoid.WalkSpeed =
    Player.TeamColor == BrickColor.new("Bright orange") and 30 or 16; 
end) 
-- Server Script Version
wait();
for index, Player in pairs(game:GetService("Players"):GetPlayers()) do 
    Player.CharacterAdded:connect(function()
    local character = Workspace:WaitForChild(Player.Name);
    if not character:FindFirstChild("Humanoid") then
        return;
    end
    character.Humanoid.WalkSpeed =
    Player.TeamColor == BrickColor.new("Bright orange") and 30 or 16; 
    end) 
end

This should work, please let me know if it doesn't or if there was anything I left out that I didn't catch.

0
It doesnt seem to do anything wjs3456 90 — 9y
Ad

Answer this question