Speed subtracter

How do I make a part which once I though it drops my speed by 20.

I’ve made this script inside of a part

script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Humanoid.WalkSpeed -=20
	end
end)

But it changes your speed into 0. I believe we have to check if our walk speed is a 0 but I am not entirely sure. If you can assist in any way please help.

Hit can be ran multiple times. If you want to check something on hit, then add a bool check to make sure it isn’t ran multiple times, and wait on it until a specific amount of time.

IE:

local bool = false
script.Parent.Touched:connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and not bool then
		bool = true
		hit.Parent.Humanoid.WalkSpeed -=20
		wait(X)
		bool = false
	end
end)

What do you mean bool. Do I add anything more besides a part and a script

A bool is a true/false statement, But other than that, I think a bool would work to help fix the speed being set to 0 immediately.

It doesn’t work. I’ve tried it and it changed my walk speed into 0

Did you make sure your walkspeed is greater than 20 before you tested it?
Or was it that you didn’t put a long enough wait time?

Nvm. I fixed it I made a mistake. Also how do you know this. How would you tell others to learn about LUA

Personally, I had to learn LUA by scratch from a diffrent programming language, I already have the neccessary skills to transfer, but for people who want to learn scripting, I think its a topic of learning the following things about coding:

Events
Functions
Effects
Variables

Usually that’s what I have my mind set on, Events are called like how a proximity prompt is triggered, or a hit detection goes off. Functions are like callable code snippets with arguments that can define what is used. Effects are what causes what to occur, like a screen going black or a part going invisible. A variable is a thing that stores data, between local/global. Global is defined as _G, and local is defined within the script with “local name = …”

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.