Scripting help speed

How to I add a cool down onto this script

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 -=18
		wait(X)
		bool = false
	end
end)

I want this cool down to be for the palyer who touched it not for everyone else in the server.

Make this a LocalScript and add a Variable for the Player at the start of you need to refer to it

How would i do that??? like this

local player = game.Players.LocalPlayer
local bool = false
local X = 0.2 -- Replace with your desired wait time in seconds or you can remove and replace x

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 -= 18
        wait(X)
        bool = false
    end
end)

If this is a Script than make it a LocalScript, as LocalScripts are, well, local

Also unnoticed you are checking for hit and hit.Parent, which is just taking away memory

How would it like like in the code?

local bool = false
local X = 2 -- Replace with your desired wait time in seconds

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

Simply put this Code in a LocalScript, I made a few corrections

okay thank you bro. How do you even know how to do this. So we can also just remove local x = 2

1 Like

Just to make sure this only works for a individual player right so the timer is put on that palyer?

If you want yes, but you would have to change Wait(X) to Wait(2) or any number you need, also I learned thsi with a little of help from the DevForum and looking around the Creator Documentation, and one tutorials

If there isn’t anything more that may interact with the other Players and change their cool-down than no, thsi should work, test it and then tell me

okay I will test it and come back okay thx tho

1 Like

There is an error this code doesn’t apply to one player instead it applies for every other player in the server.

How do I make it if the speed becomes less than 0 it becomes 1

Check out math.clamp. It allows you to keep a value in a specific range.