So im kinda a begginer scripter and idk what to do but im making a spinning part with 2 lights inside of it. if you click a button it spins but when you click it again its supposed to stop. how would i do this
local click = script.Parent
local part = game.Workspace.Spinning1.Bulb
local light1 = game.Workspace.Spinning1.Bulb.Neon.SpotLight1
local light2 = game.Workspace.Spinning1.Bulb.Neon.SpotLight2
click.MouseClick:Connect(function()
if light1.Enabled == false and light2.Enabled == false then
light1.Enabled = true
light2.Enabled = true
if light1.Enabled == true then
while true do
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(10), 0)
wait()
end
end
else
light1.Enabled = false
light2.Enabled = false
end
end)
When the button is clicked, if spinning is false, the lights turn on and the part starts spinning. If spinning is true, the lights turn off, and the part stops spinning by setting spinning = false which breaks the loop.
local click = script.Parent
local part = game.Workspace.Spinning1.Bulb
local light1 = game.Workspace.Spinning1.Bulb.Neon.SpotLight1
local light2 = game.Workspace.Spinning1.Bulb.Neon.SpotLight2
local spinning = false -- Variable to track whether the part is spinning or not
local function spinPart()
while spinning do
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(10), 0)
wait()
end
end
click.MouseClick:Connect(function()
if spinning == false then
-- Turn on the lights and start spinning
light1.Enabled = true
light2.Enabled = true
spinning = true
spinPart() -- Call the spin function
else
-- Turn off the lights and stop spinning
light1.Enabled = false
light2.Enabled = false
spinning = false
end
end)