For my game, I need to use a heartbeat to check when a block reaches a position and then return it. Should I just assign it to a variable or is it possible to return from a heartbeat?
You should be able to return from any function. Just make sure you have a bool check/debounce to make sure that it doesn’t return more than you intend since it will be constantly running with the heartbeat event.
Unfortunately Heartbeat only returns connection not the returned value unless I’m stupid and not doing it right.
If you’re referring to the RunService event named Heartbeat, the only argument passed from that event is the amount of time since the previous heartbeat event. So if you are using a function bound to heartbeat to check the location of a part, you would have to use some extra variable to see when a desired condition is met.
Here is an example that forces the script to wait until a part is in a specific place:
(the actual return value of heartbeat doesn’t need to be used here.)
local part = workspace.Part
local runservice = game:GetService("RunService")
while (part.Position.X < 10) do
runservice.Heartbeat:Wait()
end
print("the part's X position has surpassed 10")
If the script is controlling how the part moves, that is a case where you could use the value returned by the heartbeat event:
local part = workspace.Part
local speed = 1
local runservice = game:GetService("RunService")
while (part.Position.X < 10) do
local dt = runservice.Heartbeat:Wait()
part.Position = part.Position + Vector3.new(speed * dt, 0, 0)
end
print("the part's X position has surpassed 10")