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 2 years ago
Edited 2 years 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?

01local part = script.Parent
02 
03part.Touched:Connect(function(hit)
04    local humanoid = hit.Parent:FindFirstChild("Humanoid")
05    if humanoid then
06        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
07        local Sound = game.Workspace.Sounds.SpeedPowerPadAudio1:Clone()
08        local humanoid = hit.Parent:findFirstChild("Humanoid")
09        local Red = script.Parent.Parent.Yellowtored
10        Sound.Parent = Player.PlayerGui
11 
12 
13        Sound:Play()
14        part.CanTouch = false
15        humanoid.WalkSpeed = 70
View all 23 lines...

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Maybe you can do something like this example: Make a string value and place it somewhere in workspace

1Part.Touch = true
2while true do
3wait(.1)
4val + .5
5if val == 1.5 then
6Part.Touch = false
0
This doesn't do anything BunjiBloxxer 51 — 2y
0
maybe now it will work 666_brithday 103 — 2y
Ad
Log in to vote
0
Answered by
IcyEvil 260 Moderation Voter
2 years 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.

01local part = script.Parent
02 
03part.Touched:Connect(function(hit)
04local function x =
05    local humanoid = hit.Parent:FindFirstChild("Humanoid")
06    if humanoid then
07        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
08        local Sound = game.Workspace.Sounds.SpeedPowerPadAudio1:Clone()
09        local humanoid = hit.Parent:findFirstChild("Humanoid")
10        local Red = script.Parent.Parent.Yellowtored
11        Sound.Parent = Player.PlayerGui
12 
13 
14        Sound:Play()
15        part.CanTouch = false
View all 28 lines...

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 — 2y

Answer this question