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

How do I make this code restart from the beginning if a part is touched after 1.5 seconds?

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)

2 answers

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

2 comments

0

This doesn't do anything

0

maybe now it will work

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.

1 comment

0

This code gives me a 7 second speed boost when I touch it, but for some reason every 9 seconds or so after I have touched the part once it gives me another speed boost, even if I am not touching the part., and it doesn't stop

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