How to stop a RunService loop

I’m currently making a game which involves and “auto run” button, which when activated spawns a parts and makes the player walk to it.

I have tried many things like putting the loop in a coroutine or making it a variable and using :Disconnect() but nothing seems to work. My code also involves changing the buttons colour to (which works).

I am relatively new to scripting so sorry if this a stupid mistake that I have made. This is my code

-- SERVICES
local runService = game:GetService("RunService")
local Debris = game:GetService("Debris")

-- VARIABLES
local button = script.Parent
local red = Color3.new(1, 0, 0)
local green = Color3.new(0, 1, 0)
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:FindFirstChild("Humanoid")
local hrp = character:FindFirstChild("HumanoidRootPart")

--COROUTINE
local runCoroutine = coroutine.create(function()
	runService.Heartbeat:Connect(function()
		local newPart = Instance.new("Part")
		newPart.CanCollide = false
		newPart.Position = Vector3.new(hrp.Position.X - 10, hrp.Position.Y , 0)
		newPart.Parent = workspace
		newPart.Anchored = true
		newPart.Transparency = 1

		humanoid:MoveTo(newPart.Position)

		game.Debris:AddItem(newPart, 0.5)
	end)
end)



-- MAIN
button.Activated:Connect(function()
	if button.BackgroundColor3 == red then
		button.BackgroundColor3 = green
		task.spawn(runCoroutine)
	elseif button.BackgroundColor3 == green then
		task.cancel(runCoroutine)
		button.BackgroundColor3 = red
	end
end)

Any help would be much appreciated!

1 Like

You don’t store the thread, you store the connection itself, then call :Disconnect() on it to stop it!

1 Like

how would I go about doing that?

1 Like
local runConnection = runService.Heartbeat:Connect(...)

Then, later,

runConnection:Dieconnect()
1 Like

I don’t know if I’ve done it wrong but it still isn’t working. This is my code

button.Activated:Connect(function()
	if button.BackgroundColor3 == red then
		local runConnection = runService.Heartbeat:Connect(function()
			local newPart = Instance.new("Part")
			newPart.CanCollide = false
			newPart.Position = Vector3.new(hrp.Position.X - 10, hrp.Position.Y , 0)
			newPart.Parent = workspace
			newPart.Anchored = true
			newPart.Transparency = 1

			humanoid:MoveTo(newPart.Position)

			game.Debris:AddItem(newPart, 0.5)
			
			if button.BackgroundColor3 == green then
				runConnection:Disconnect()
			end
		end)
	end
end)

red and green are variables btw

1 Like

If you want to disconnect a connection within itself, you need to have a separate variable and then assign it later;

local runConn = nil
runConn = runService.Heartbeat:Connect(...)

And then disconnect it within the connection

2 Likes

thank you so much man i finally got it working. This is the final code if you are interested

button.Activated:Connect(function()
	if button.BackgroundColor3 == red then
		button.BackgroundColor3 = green
		runConnection = runService.Heartbeat:Connect(function()
			local newPart = Instance.new("Part")
			newPart.CanCollide = false
			newPart.Position = Vector3.new(hrp.Position.X - 1, hrp.Position.Y , 0)
			newPart.Parent = workspace
			newPart.Anchored = true
			newPart.Transparency = 1

			humanoid:MoveTo(newPart.Position)

			game.Debris:AddItem(newPart, 0.1)
		end)
	elseif button.BackgroundColor3 == green then
		button.BackgroundColor3 = red
		runConnection:Disconnect()
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.