I have this code that increases the player's walkspeed to 70 when they touch a part. I want this code to give the player a speed boost for 7 seconds when they touch it, but I want them to be able to renew that speed boost after 1.5 seconds.
Basically, if they touch the part after 1.5 seconds has gone by, the code restarts from the beginning and they have 7 seconds of speed boost again. How can I do that?
local part = script.Parent part.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) local Sound = game.Workspace.Sounds.SpeedPowerPadAudio1:Clone() local humanoid = hit.Parent:findFirstChild("Humanoid") local Red = script.Parent.Parent.Yellowtored Sound.Parent = Player.PlayerGui Sound:Play() part.CanTouch = false humanoid.WalkSpeed = 70 Red.BrickColor = BrickColor.Red() wait(1.5) Red.BrickColor = BrickColor.new("Deep orange") wait(5.5) part.CanTouch = true humanoid.WalkSpeed = 25 end end)
Maybe you can do something like this example: Make a string value and place it somewhere in workspace
Part.Touch = true while true do wait(.1) val + .5 if val == 1.5 then Part.Touch = false
Something like this should work, basically you make the entirety of what you wrote as a separate function entirely, and use a while wait() loop to run the script after 1.5 seconds after being touched.
local part = script.Parent part.Touched:Connect(function(hit) local function x = local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) local Sound = game.Workspace.Sounds.SpeedPowerPadAudio1:Clone() local humanoid = hit.Parent:findFirstChild("Humanoid") local Red = script.Parent.Parent.Yellowtored Sound.Parent = Player.PlayerGui Sound:Play() part.CanTouch = false humanoid.WalkSpeed = 70 Red.BrickColor = BrickColor.Red() wait(1.5) Red.BrickColor = BrickColor.new("Deep orange") wait(5.5) part.CanTouch = true humanoid.WalkSpeed = 25 end end while wait(1.5) do x() end end)
Probably not exact, but you should get the idea.