Help! Why my loop is repeating every frame!

i’ve done a post yesterday but now there is another bug, the deer is moving every game frame!
here’s the video of the problem: robloxapp-20240225-1305180.wmv (1021.0 KB)

here’s the script that is controlling the movement:

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 WalkCycleConnection
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

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

--Calling Functions--

coroutine.wrap(SoundsLoop)()
WalkCycleConnection = RunService.Heartbeat:Connect(WalkCycle)
--coroutine.wrap(FindPlayersInRange)()

please respond.

When using RunService.Heartbeat, it fires the function WalkCycle every single game frame. Another thing to note is that there is a while true loop inside of WalkCycle. Having multiple while true loops being created every heartbeat is extremely bad for game performance.

For now, just launch WalkCycle as WalkCycle(), there is not need to call the function every heartbeat.

but i need the walk cycle to stop

I see what you mean and what you tried to do with WalkCycleConnection:Disconnect(), but that only disconnects WalkCycle from RunService.Heartbeat, the while true loop still runs.

From the context I have, I assume the deer stops its movement when it finds a player in range. You could do the following:

local isMoving = false
local playerInRange = false

function WalkCycle()
    isMoving = true
	while not playerInRange 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
    isMoving = false
end

function FindPlayersInRange()
    -- No idea how you are detecting if a player is in range, but use foundPlayer as an example
    if foundPlayer then
        playerInRange = true
    else
        playerInRange = false
        if not isMoving then
            WalkCycle()
        end
    end
end

im gonna try to see if it works

it doesn’t work:
robloxapp-20240225-1342324.wmv (1.6 MB)

Can you send your current script with the changes added?

--Services--

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--Variables--

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

local isMoving = false
local playerInRange = false
local foundPlayer = nil

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

function WalkCycle()
	isMoving = true
	while not foundPlayer 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
	isMoving = false
end

function FindPlayersInRange()
	deer.Trigger.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") then
			foundPlayer = Players:GetPlayerFromCharacter(hit.Parent)
		end
	end)
	if foundPlayer then
		playerInRange = true
	else
		playerInRange = false
		if not isMoving then
			WalkCycle()
		end
	end
end

--Calling Functions--

coroutine.wrap(SoundsLoop)()
WalkCycleConnection = RunService.Heartbeat:Connect(WalkCycle)
--coroutine.wrap(FindPlayersInRange)()

the problem is that is bugging the deer because it goes crazy
and then the disconnect doesn’t work anymore