Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago
Edited 1 year ago

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

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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
0
This doesn't do anything BunjiBloxxer 51 — 1y
0
maybe now it will work 666_brithday 103 — 1y
Ad
Log in to vote
0
Answered by
IcyEvil 260 Moderation Voter
1 year ago

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.

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 BunjiBloxxer 51 — 1y

Answer this question