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
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)
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
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)