Heartbeat (Event)

From Legacy Roblox Wiki
Revision as of 18:06, 24 January 2012 by >Builder1010101 (Ok, this is my second "de-stubbing". Please be critical. I did my best to make it detailed, please tell me if it is too complicated/ meandering!)
Jump to navigationJump to search
Heartbeat ( Float Seconds )
Description This event constantly happens. A "heartbeat" usually happens approximately every 1/30th of a second.
Member of: RunService


The following code would be put in a Script. The script can be placed in the Workspace.

local service = game:GetService("RunService")
local variable2 = 5
wait(5)
service.Heartbeat:connect(function(variable1)
    if variable2 > 0 then
        variable2 = variable2 - 1
        print("The function returns: "..variable1)
    end
end)

The function returns: 0.033333335071802 The function returns: 0.033333335071802 The function returns: 0.033333335071802 The function returns: 0.033333335071802

The function returns: 0.033333335071802

This code does the following:

  • The first line gets the Service RunService and puts it in the Variable "service".
  • The second line creates the local variable "variable2" and sets its value to 5.
  • The third line uses the wait function, which delays the script for 5 seconds. This is necessary because the "heartbeat" does not run regularly at the start of a game.
  • The fourth line calls the function below when a "heartbeat" happens with the parameter "variable1", which is equal to the time between each heartbeat. The "end)" on the ninth line ends the function.
  • The fifth, sixth and eighth line serve as a stopper. If they weren't there, the code inside of the if statement would be repeated every thirtieth of a second until the end of the game.
  • The seventh line prints "The function returns: (variable1, which is 0.033333335071802)". It uses concatenation, which basically adds a variable to a string.