Hi. I would like to know if this is an appropriate use of Heartbeat.
My goal is to create tiles that will fall from the sky during the round. This is the best way I could imagine to do this. This does work, but I need to know the best way to do this.
From what I understand, using Heartbeat on the server means it is not affected by the client’s frame rate therefore it will run 60 times per second. However, if I want to create a tile when the amount of time interval
has passed then would a while loop be more effective as it would only be looping whenever the amount of time interval
has passed as opposed to Heartbeat which would still run 60 times per second? (I guess?)
For example:
while can_Run do
task.wait(interval)
-- creating tile
create_Tile()
end
I see many developers saying that they never use while loops because they are “inefficient” or that they “can’t remember the last time they had a use for one”. The negative things I’ve read of them is the reason why I try to stay away from them, but I don’t understand why they are “bad”.
Any help/feedback would be greatly appreciated.
local game_Module = {}
local rep_Storage = game:GetService("ReplicatedStorage")
local run_Service = game:GetService("RunService")
local game_Status = rep_Storage.Values.game_Status
local round_Status = rep_Storage.Values.round_Status
-- tile config
local tile_Debounce = false
local interval -- this will change as the round progresses
function create_Tile()
print("running")
if not tile_Debounce then
tile_Debounce = true
-- creating tile (just printing for testing)
task.wait(interval)
print("creating tile")
tile_Debounce = false
end
end
function game_Module.game_Loop(dur)
interval = 1
local tile_Connection = run_Service.Heartbeat:Connect(create_Tile)
for i = dur, 0, -1 do
round_Status.Value = i
task.wait(1)
end
tile_Connection:Disconnect()
end
return game_Module