Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What loop to use when CFraming part around player?

Just a background; I've been scripting for about a year now although I'm far from good as I experience alot of problems even on simple things, I find problems sometimes hard to catch up for me. Anywho,

I've been wanting to make multiple parts manipulate around a player like so:

https://gyazo.com/0abb40948a54c613b3fb26f112698987

(the work of a player named literomancer)

And granted they're parts being CFramed around the player, looping, what is the efficient way to looping parts being CFramed nowadays? Like, what loop do I use to achieve an efficient output?

I've tried using the RunService, but I don't even know, I found articles that really made me doubt about it.

Thanks!

1 answer

✓ Accepted answer

#Events

RunService.RenderStepped runs every frame.

local connection;
connection = game:GetService('RunService').RenderStepped:Connect(function(dt)
	if (bad) then connection:Disconnect() end;
end);

RunService.Heartbeat runs every frame after the physics simulation completes.

local connection;
connection = game:GetService('RunService').Heartbeat:Connect(function(dt)
	if (bad) then connection:Disconnect() end;
end);

RunService.Stepped runs every frame prior to the physics simulation.

local connection;
connection = game:GetService('RunService').Stepped:Connect(function(dt)
	if (bad) then connection:Disconnect() end;
end);

Loops

An iterator loop runs for every incremention of x to y via incrementor z.

for i = 0, 1, 0.5 do
	...
end

A while loop runs until the given condition is truthy.

while ... do

end

A function loop is bad:

function isbad() 
	this() 
end 
function this() 
	isbad(); 
end
function this_is_bad() 
	this_is_bad(); 
end

A repeat loop runs until the given condition provided after the until keyword is truthy.

repeat

until not bad;

#Warning and Trivia Make sure you provide a yielding function for your non-event loops. They will crash your game.


In the script builder community, which is typically centered around CFrame animation, they use a for loop. This is because CFrame:Lerp, the Linear, intERPolation function requires an alpha that signifies the progress between CFrameA and CFrameB, which can easily be given through for i = 0, 1, 0.1 do as i would be the given progress.


wait() runs according to the built-in 30hz roblox scheduler. Recommended not to use for quick action.

This discussion is preserved as it was. New answers, comments, and votes are closed.