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

Is there another way of doing a loop without using "While true do?"

Asked by
ImfaoXD 158
6 years ago

I made this spinning birds that spins around and I'm satisfied with it. But it lag my game though, is there a way to prevent the lag from happening? Can anyone help me?

Here is my layout of the model:

https://gyazo.com/9cf296413be0a2b7677fcf670685ad35

If you need more details, here is a short clip of how the model works:

https://gyazo.com/9e345e8d5a59f9eade4b7cfaf76ba35d

Finally, this is the script that make the model spin:

local function rotate_model(model,angle)
    local center = model:GetModelCFrame()
    local part = Instance.new("Part")
    part.CFrame = center
    model.PrimaryPart = part
    model:SetPrimaryPartCFrame(center*angle)
    part:Destroy()  
end

local tornado = script.Parent

while wait(.03) do
    rotate_model(tornado,CFrame.Angles(0,0.1,0))
end

2 answers

Log in to vote
2
Answered by 6 years ago

Ok, so there are three types of loops.

FOR Loops...

for i = 1, 5 do
-- Insert script here
end

They will do things a set amount of times. This script does it 5 times.

REPEAT Loops...

local i = 1
repeat
  -- Insert script here
   i = i + 1
until i==10

This will repeat until a variable has met a specific value. This will, for example, loop until i = 10.

WHILE Loops...

local i = 1
while i < 10 do
   i = i + 1
end

This will repeat as long as i is less than 10. If it is over the value of 10, the loop will stop. I hope this helped. I didn't want to just give you the answer because it's much better to learn from experimentation rather than copy pasting.

Peace.

0
If you need more elaboration, check out the Roblox Wiki post. http://wiki.roblox.com/index.php?title=Loops OrcaTheFish 95 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

The different types of loops achieve different goals, but you can treat them as all equally efficient (assuming you aren't repeating within the loop more than you need to).

You could improve the efficiency of your rotate_model slightly by not remaking the part every time (make it a global variable and do not destroy it). You could also rotate the model in-place instead of moving it around a bunch (but this only works if the model has a PrimaryPart; you'd use Set/Get PrimaryPartCFrame in that case).

You should remove the argument to wait and see if that improves that "lag" you are exper

If the bird model is very complex, that might explain why you're lagging. If by "lag" you mean that the model isn't rotating smoothly, possibly:

  • The wait command is sometimes waiting for .06 or so (the solution is to just call wait() not wait(0.03), so if Roblox tries to return after 0.029, it won't wait another .03 before resuming)
  • If you want super smooth animations, consider using Heartbeat
  • if the bird model is extremely complicated, perhaps that would be enough to lag your place?

Answer this question