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

How do you stop a loop if you walk off the part?

Asked by 4 years ago

Hi, I am making a treadmill for my weight lifting simulator, but the loop never stops when I step off the treadmill. I've tried to make a true or false statement but it did not work. Anybody have a possible solution?

local character = game.Players.LocalPlayer.Character.Humanoid
local speed = game.ReplicatedStorage.Speed
function onTouched(hit)
    while true do
    speed.Value = speed.Value + 10
    wait(1)
    speed.Value = speed.Value + 10
    wait(1)
end
end
script.Parent.Touched:connect(onTouched)

if character = nil then 
    return
end
0
I am pretty sure there is an event called TouchEnded. Try putting that into use! zane21225 243 — 4y
0
Maybe try using "Until"? Galaxybombboy 134 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Re-write your script with mine and what it will do is when you step off the treadmill the loop will stop.

local character = game.Players.LocalPlayer.Character.Humanoid
local speed = game.ReplicatedStorage.Speed
local db = true

function stop()
    db = false
    wait(2)
    db = true
end

function onTouched(hit)
    while true do
        speed.Value = speed.Value + 10
        if db == false then
            break
        end
        wait(1)
        speed.Value = speed.Value + 10
        if db == false then
            break
        end
        wait(1)
        if db == false then
            break
        end
    end
end

script.Parent.Touched:connect(onTouched)
script.Parent.TouchEnded:Connect(stop)

if character = nil then
    return
end

I hope that helps :D

Ad

Answer this question