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

Block that slows you down?

Asked by 5 years ago
Edited 5 years ago

Is it possible to have a brick that slows players down when stepping on it and also slows down sprinting/running?

0
Next time, try and put your own research into a topic before coming here. We most of the time only help with existing code that does not work. zblox164 531 — 5y

2 answers

Log in to vote
0
Answered by
tzmods 59
5 years ago

The Script

local part = game.Workspace.Part -- this identifies the part that will be touched
local walkspeed = 10 -- this is the walkspeed that the humanoid will be set too when comming in contact with the part
local oldwalkspeed = 0 -- Don't change this (this saves your walkspeed that you had before touching the part, so that we can give it back to you later.
local debounce = false -- debounces are used to make the code run only when in an idle state (idle as in not being ran)
local debounce2 = false

part.Touched:Connect(function(hit) -- fires the function when the part is touched. (hit) defines the player that touched it.
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 and hit.Parent.Humanoid.WalkSpeed ~= walkspeed then -- this checks if whatever touched it has a humanoid (all players have a humanoid) and if the player is not dead (has more then 0 health) as well as checking if the player already has there speed affected
        if debounce == false then -- checks if the debounce is false
            debounce = true -- makes the debounce true so that when the if statement above checks if it is false it won't be so the code will not fire again.
            oldwalkspeed = hit.Parent.Humanoid.WalkSpeed -- this changes oldwalkspeed to the walkspeed you had before touching the part
            hit.Parent.Humanoid.WalkSpeed = walkspeed -- this sets the player who touched the parts walkspeed to the same value as walkspeed.
            wait(1) -- waits 1 second before continuing the script
            debounce = false -- set's the debounce to false so that the function above can be ran again
        end -- ends the debounce if/then statement
    end -- ends the player check and health check if/then statement
end) -- ends the .Touched function

part.TouchEnded:Connect(function(hit) -- fires when the part stops being touched.
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Humanoid.Health > 0 then
        if debounce2 == false then
            debounce2 = true
            wait(5) -- waits 5 seconds before continuing reading the code
            hit.Parent.Humanoid.WalkSpeed = oldwalkspeed -- sets the players walkspeed to the same walkspeed they had before touching the part.
            wait(1)
            debounce2 = false
        end
    end
end)

Study this code. I left you notes for a reason.

Ad
Log in to vote
1
Answered by
zblox164 531 Moderation Voter
5 years ago

The fix

Just detect when the player collides with the block. Then get their character from the collision. After that, you can find the humanoid and slow their Walkspeed. This can be done using the Touchedevent.

Here is some information you can check out:

Resources

Touched Event

RbxScriptSignals (aka events)

Walkspeed

Not giving a script since the resources above give examples.

Hope this helps!

0
If you have a sprint script, then just check to make sure your not colliding with the block before the sprint. zblox164 531 — 5y

Answer this question