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

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

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

2 comments

0

I am pretty sure there is an event called TouchEnded. Try putting that into use!

0

Maybe try using "Until"?

1 answer

✓ Accepted answer

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

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