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

Help with RenderStepped function with CFrame?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

I am not sure on how to use the RenderStepped function. I am not sure if I would have to use a wait or what but I though RenderStepped was a type of "wait()".

local Plr = game.Players.LocalPlayer
local Part = game.ReplicatedStorage:WaitForChild("DisplayInfo"):Clone()
Part.Parent = Plr.Character
Part.CanCollide = false

game:GetService("RunService").RenderStepped:connect(function() --Instead of this, I used a wait and it worked fine. It is my first time using this function.
    Part.CFrame = Plr.Character:WaitForChild("Head").CFrame * CFrame.new(0,3,0)
    Part.Anchored = true
end
3
RenderStepped basically just runs the code inside the function for as long as the Roblox Player window is focused, for example: I minimize Roblox to view my desktop- The code will not run until I re-focus on the Roblox Player window. UniversalDreams 205 — 8y
1
Ahhh ok thanks! FiredDusk 1466 — 8y
0
I'm pretty sure that if there were 2 clients, one with a framerate of 60, and one with a framerate of 10, if you were to use the renderstepped function they would be firing the function at different rates. UltimateRaheem 75 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago

Wiki entry for the RunService.

Every ~1/60 of a second, a render update happens. Each render update, a bunch of code is run, namely the code involved with rendering the 3D environment on your screen. There's other stuff too, but that is the "rendering" that the name refers to.

If you want to run your own code on each render update, you have 2 options. You can connect to an event, which is the "old way:"

RunService.RenderStepped:connect(function(step)
    -- code goes here
    -- here "step" is the exact amount of time since the last render update
end)

Or you can bind a callback, which is the "new way:"

local function onRenderStepped()
    -- code goes here
end

RunService:BindToRenderStepped("renderFunctionName", Enum.RenderPriority.First.Value, onRenderStepped)

-- corresponding unbind:
RunService:UnbindFromRenderStepped("renderFunctionName")

The new way is basically a fancier version of the old way, they do the same thing but the new way is easier and safer to use, in my opinion.

The render priority enum determines in what order render functions are executed. Higher numbers mean executed later. The wiki entry for the RenderPriority enum is here.

The functions you bind to render stepped are gonna be called really fast, faster than you can make the be called in a normal loop, because wait() can only delay for as little as 1/30 of a second. Make sure whatever functions you bind to render stepped are really fast, because if they aren't, they will actually slow down render updates, making your game lag like crazy.

I want to emphasize that these functions are called only on render updates, not on a 1/60 of a second loop. If no render update happens, your function wont be called. This means if the game is minimized, you wont get render updates.

I hope this helps!

1
You can't assume RenderStepped is going to be called anywhere near every 1/60 of a second. Console devices will get closer to 1/30, and different PC and mobile devices will get wildly varying framerates depending on available hardware. P100D 590 — 7y
0
Yes, note the last paragraph. script_help 74 — 7y
Ad

Answer this question