Setting WalkSpeed to 0 when touched

Hello! I thought this would be a lot easier, but it sadly was not for me as I have been trying for the last 10 minutes.

How would I set a players walk speed to 0 when they touch a certain part? I understand the Touched event but just can’t get it to work.

3 Likes

@iy_uhn
Touched Events can’t be detected when using a Local Script, unless the local script is parented to the player, PlayerGUI (Starter Gui before instantiated) or PlayerScripts, StarterCharacterScripts.

Make sure you’re using a Server Script to detect the .Touched event.

Edit: Can we see your code to tell you what’s wrong?

1 Like

I’m not using a LocalScript. I’m using a ServerScript.

2 Likes

This is how it would go.

local debounce = false

part.Touched:Connect(function(hit)
    if not debounce and hit.Parent:FindFirstChild("Humanoid") then
        debounce = true
        hit.Parent.Humanoid.WalkSpeed = 20 --Sets walkspeed
        wait(5)
        debounce = false --Ends debounce
    end
end)

Edit: Added a debounce.

8 Likes

@GalaxyGourmet Unless you add some sort of Debounce to that code, it will fire hundreds of times, which is not something you want.

Edit : I see you added debounce, but not the right way.

And by the way,
Just dumping code here without any explanation won’t help the OP at all, unless you know or at least, are explaining what the lines of code actually do.
@C0lvy123 on the other hand, that’s a satisfactory explanation!

1 Like

A player’s character is just a model with a bunch of limbs, customization stuff, and a humanoid. Whenever a player touches a part, it’s one of their limbs that touches it. Like the bottom half of their leg or their torso. So to check if it was a player that touches a part, all you have to do is check if it is “Siblings” with a humanoid. For example:

Part.Touched:Connect(function(Hit)
-- Hit represents the part that made the touched event fire 
if Hit.Parent.Humanoid then
print("A player's limb touched the part")
end
end)

Now all you have to do is change the player’s humanoid’s WalkSpeed property to 0, like this:

Part.Touched:Connect(function(Hit)
if Hit.Parent.Humanoid then
Hit.Parent.Humanoid.WalkSpeed = 0
end
end)

(Sorry if I didn’t explain it that well)

This is basically what someone’s character looks like:

game < Workspace < C0lvy123 < Torso, LeftLeg, RightLeg, Head, Humanoid, Hat

4 Likes

This makes no sense, because if there’s no humanoid the script will error. You should use :FindFirstChild(“Humanoid”) instead.

However, that was a nice explanation! Just dumping code there wouldn’t have helped op at all.

4 Likes