How do i stop a function without using "Connect"

i need to stop a function but idk what to do here’s what i’ve done so far:

local RunService = game:GetService("RunService")

local deer = script.Parent
local humanoid = deer:FindFirstChildWhichIsA("Humanoid")
local rootPart = deer:FindFirstChild("HumanoidRootPart")
local Animations = deer.Animations
local Values = deer.Values

local Idle = humanoid.Animator:LoadAnimation(Animations.Idle)
local Walk = humanoid.Animator:LoadAnimation(Animations.Walk)
local Charge = humanoid.Animator:LoadAnimation(Animations.Charge)

local PosX, PosY, PosZ
local Deer_Groan = rootPart.Deer_Groan
local Deer_Angry = rootPart.Deer_Angry
local PlaybackSpeed

--Functions--

function FindPlaybackSpeed(oldSpeed)
	local Speed = math.random(2, 4) / 3
	print(oldSpeed, Speed)
	if Speed == oldSpeed then
		return FindPlaybackSpeed(oldSpeed)
	else
		return Speed
	end
end

function SoundsLoop()
	while RunService.Stepped:Wait() do
		local PlaybackSpeed = FindPlaybackSpeed(Deer_Groan.PlaybackSpeed)
		Deer_Groan.PlaybackSpeed = PlaybackSpeed
		Deer_Groan:Play()
		Deer_Groan.Ended:Wait()
		wait(math.random(5, 10))
		--wait(0.5)
	end
end

--[[local WalkCycle = 
		wait(math.random(3, 5))
		Idle:Stop()
		wait(0.1)
		Walk:Play()
		PosX = rootPart.Position.X + math.random(-5, 5)
		PosY = rootPart.Position.Y + math.random(-5, 5)
		PosZ = rootPart.Position.Z + math.random(-5, 5)
		humanoid:MoveTo(Vector3.new(PosX, PosY, PosZ))
		humanoid.MoveToFinished:Wait()
		Idle:Play()
		wait(0.1)
		Walk:Stop()
end)]]--

function FindPlayersInRange()
	WalkCycle:Disconnect()
end

--Calling Functions--

coroutine.wrap(SoundsLoop)()
coroutine.wrap(WalkCycle)()
wait(5)
coroutine.wrap(FindPlayersInRange)()

i don’t really have much information…

:smiling_face_with_tear:

Which function are you trying to stop?

1 Like

the walk cycle function because i need to start a charge cycle

1 Like

You can stop a function using coroutine.close() or coroutine.yield() You just need to get the Thread first by using coroutine.create().

local thread = coroutine.create(WalkCycle)
coroutine.resume(thread) -- Executes the function

Then you can use that exact same thread to stop it or yield it.

It seems you’re trying to stop the function named WalkCycle.

Now WalkCycle is a function that runs indefinitely in a loop. It’s connected to the Heartbeat event of RunService. When you need to stop it, you call FindPlayersInRange(), which disconnects the WalkCycle function from the heartbeat event, effectively stopping it.

Let me know if this is what you needed and if this worked properly.

local WalkCycleConnection

function WalkCycle()
    while true do
        wait(math.random(3, 5))
        Idle:Stop()
        wait(0.1)
        Walk:Play()
        PosX = rootPart.Position.X + math.random(-5, 5)
        PosY = rootPart.Position.Y + math.random(-5, 5)
        PosZ = rootPart.Position.Z + math.random(-5, 5)
        humanoid:MoveTo(Vector3.new(PosX, PosY, PosZ))
        humanoid.MoveToFinished:Wait()
        Idle:Play()
        wait(0.1)
        Walk:Stop()
    end
end

function FindPlayersInRange()
    if WalkCycleConnection then
        WalkCycleConnection:Disconnect()
    end
end

WalkCycleConnection = RunService.Heartbeat:Connect(WalkCycle)

coroutine.wrap(SoundsLoop)()
wait(5)
coroutine.wrap(FindPlayersInRange)()
1 Like

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