Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to use HeartBeat properly?

Asked by
manith513 121
2 years ago

I was trying to make a house where each part of the building disappears one by one. I was using a wait command in the for loop and realized it was too slow, and I learned that using HeartBeat did it per frame and would be faster.

local house = script.Parent
local runservice = game:GetService("RunService")

for i,v in pairs(house:GetChildren()) do

    for i = 1,2 do
        v.Transparency = v.Transparency + 0.5
     wait(runservice.Heartbeat)

    end

end

This is my code that uses the HeartBeat. I made an identical one to this except made wait() blank. After running both of them they both were the same speed. Can someone let me know how to use HeartBeat properly?

1 answer

Log in to vote
1
Answered by
NykoVania 231 Moderation Voter
2 years ago

This is how the hearbeat loop is used.

game:GetService("RunService").Heartbeat:Connect(function()
--script here
end)
0
I was doing research and learned this fired whatever was fired inside every frame. But your response helped me learn how to do it properly by calling in the wait where it was so that it wouldn't keep running and would be much faster, so I will mark your answer as the solution. manith513 121 — 2y
Ad

Answer this question