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

Why won't this walkspeed script function?

Asked by 4 years ago

Hi, I have a script that sets the walkspeed of the player when they enter the game depending on the team they are on however it doesn't seem to function and I can't identify the problem.

Here is the script:

plr = script.Parent
function onSpawn(Char)
if game.Players:FindFirstChild(Char.Name)~=nil then
if plr.TeamColor==BrickColor.new("Really black") then
Char.Humanoid.WalkSpeed = 0
end
end
game.Workspace.ChildAdded:connect(onSpawn)
0
What is the purpose of setting WalkSpeed to 0 exactly? I ask because this is exploitable, a client script can set walkspeed to something else and the player will still be able to walk around, with their movement replicated to the server. EmilyBendsSpace 1025 — 4y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
4 years ago

For first, its better to detect when Player added and Character added.

And in your code have a deprecated function: :connect, change it to :Connect

To detect when player and character added have a function for that:

game.Players.PlayerAdded:Connect(function(Player)
    print("Player", Player.Name, "Joined the game!")
    Player.CharacterAdded:Connect(function(Character)
        print("Character of", Player.Name, "Spawned!")
    end)
end)

In your case, you only need to check team, then wait for Humanoid and set speed to 0 Fixed script:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        if Player.TeamColor == BrickColor.new("Really black") then
            Character:WaitForChild("Humanoid").WalkSpeed = 0
        end
    end)
end)

Hope it helped! :)

Ad

Answer this question