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)()
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)()