Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Block that slows you down?

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

1 comment

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.

2 answers

✓ Accepted answer

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.

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 Touched event.

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!

1 comment

0

If you have a sprint script, then just check to make sure your not colliding with the block before the sprint.

This discussion is preserved as it was. New answers, comments, and votes are closed.