CFrame part issues

I’ve been trying to make a function where a part starts spinning and overtime it gets faster and faster. If a local player dies then that part stops and starts spinning again at a slow pace and then gets faster overtime. Here I have a function in a local script.

local Beam = game:GetService("ReplicatedStorage"):WaitForChild("Beam"):Clone()
Beam.Parent = workspace.CurrentCamera

local function Move()
	local speed = 0.001
	local something = true
	while something do
		task.wait()
		Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
		speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
		Player.Character.Humanoid.Died:Connect(function()
			wait(2)
			something = false
		end)
	end
end

The part which is the beam starts spinning when the function is called and gets faster and faster overtime. The only problem I’m facing is whenever the local player dies that the part doesn’t stop and doesn’t start spinning again at a slow pace. I was wondering how I could possibly fix this. Thank you.

You could try moving the Humanoid.Died connection out of the loop and define it above

local Beam = game:GetService("ReplicatedStorage"):WaitForChild("Beam"):Clone()
Beam.Parent = workspace.CurrentCamera

local function Move()
	local speed = 0.001
	local something = true

    Player.Character.Humanoid.Died:Connect(function()
		wait(2)
		something = false
	end)

	while something do
		task.wait()
		Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
		speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
		
	end
end
1 Like

Thanks for the response. Most of it works except whenever the player dies the beam stops but doesn’t start spinning again. I’m trying to make it where when a player touches a part that a beam starts spinning at a slow pace and gets faster overtime. But whenever that player dies then the beam stops and then again starts rotating at a slow pace and gets faster.