Trying to rotate parts with In pairs loop

Continuing the discussion from Trying to rotate parts with In pairs loop:

I received answers but none of them helped and so far I haven’t received any new ones and still so far I am not able to solve the problem.

The theory I made is correct, the script works but the problem is that it is causing a lot of lag and is taking time to spin up.

Script (located in a Click Detector):

local alarms = {}
for i,v in pairs(workspace.AlarmS1:GetDescendants()) do
	if v:IsA('PointLight') or v:IsA('SurfaceLight') or v:IsA('SpotLight') then
		table.insert(alarms,v)
	end
end
for i,v in pairs(alarms) do
	Bool.Changed:Connect(function()
		if Bool.Value == true then
			while true do
				wait(2)
				print("Yes")
				v.Parent.Orientation = v.Parent.Orientation + Vector3.new(1, 0, 0)
			end
		end
	end)
end

image

1 Like

Problem here could be that you are putting a while loop inside an event for every alarm there is. You should change up the order like this

Bool.Changed:Connect(function()
	while Bool.Value do
		task.wait(2)
		for i, v in pairs(alarms) do
			    print("Yes")
		        v.Parent.Orientation  =+ Vector3.new(1, 0, 0)
		end
	end
end)

3 Likes

slight mistake on your snippet, =+ is not a valid operator; Did you mean +=?

1 Like

Its still lagging, maybe be cause there so many alarms and thats causing the lag?

1 Like

I’m assuming that the prints are the ones making the lag

1 Like

Never though that prints can make lag, I will remove them and test then

1 Like

You are rotating them every 2 seconds, so i believe that is what you might confuse for lag? Instead of while loop you could use tweenservice to make it work:

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(
	1,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	-1
)

for i,v in pairs(alarms) do
	Bool.Changed:Connect(function()
		if Bool.Value == true then
			tweenservice:Create(v.Parent,info,{CFrame = v.Parent.CFrame * CFrame.Angles(0,0,math.rad(180))}):Play()
		end
	end)
end
3 Likes

It lags because every 2 seconds, the code simultaneously printing “Yes” on every iteration

1 Like

It got better, however seems that still taking some time to rotate complety.

1 Like

You might want to consider using tween service like @realmile suggested.

1 Like

Well, now the alarms are in the roof for any reason.
image

Actualy, I think I know what hapenning


try changing this part, i believe this should work:

CFrame.Angles(math.rad(180),0,0)
1 Like

Isnt lagging anymore however they are not spinning complety, maybe if I change it to 360, may resolve the problem?

nvm, changed it to 360 and they arent moving now, gonna put 180 again

I believe 180 should be the best number to make it rotate fully, 360 wouldnt rotate it at all since its a full rotation.

robloxapp-20220819-1423269.wmv (317.9 KB)

They decide go back up and then down.

Nvm, I tested again and worked fine.

It could have something to do if your script is on the server, you should be handling that on the client, so you dont stress out the server too much.

Well, anyways. Thanks you for your help.

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(
	1,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.In,
	-1
)

local alarms = {}
for i,v in pairs(workspace.AlarmS1:GetDescendants()) do
	if v:IsA('Light') then
		table.insert(alarms,v)
	end
end

Bool.Changed:Connect(function(v)
	if v then
		for i,v in pairs(alarms) do
			task.spawn(function()
				local tween = tweenservice:Create(v.Parent,info,{CFrame = v.Parent.CFrame * CFrame.Angles(math.rad(359),0,0)})
				tween:Play()
				tween.Completed:Wait()
				v.Parent.CFrame *= CFrame.Angles(0,0,0)
			end)
		end
	end
end)

https://developer.roblox.com/en-us/api-reference/class/Light
‘Light’ is the base class for light objects.

1 Like