Memory issue ( i guess)

i have some questions

  1. when there is a heartbeat event that runs every frame and it calls a specifc function when it meets a specific condition, like here

function test()
for i=1,10 do
if var == false then return end
print("you")
end
-- rest of the function
end

heartbeat:Connect(function() -- well imagine some conditions here for this heartbeat to work 
if var2 == true then
test()
end
end)

now lets pretend that the var boolean somehow get changed in the script to false will that return statement remove the next running test functions from the memory or no?

2.how to make a function called from a heartbeat event stop when a certain condition meet

i’m not exactly sure what you mean now.
You can connect the heartbeat event when you need it and disconnect it again when you don’t want it to run anymore.

local RunService = game:GetService("RunService")

print("Starting heartbeat")

local connection = RunService.Heartbeat:Connect(function()
	print("Heartbeat")
end)

task.wait(5)

print("Disconnecting heartbeat")
connection:Disconnect()

as for the loop. as long as you don’t change var inside the loop or you don’t have anything that yields in the loop it is impossible for other parts of the code to change var while the loop is running.

I hope that answered your question, if not could you rephrase it?