Function works once but then doesn't work after

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

	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
   Move()
end

The problem is the function works the first time but not anytime after that.

You could see that when the player resets the first time the beam stops and starts spinning slowly and then speeds up. But if the player resets a second time after that then that same thing wont happen. I was wondering how too fix this, thanks.

1 Like

Put this code in StarterCharacterScripts …
image

local Beam = workspace.CurrentCamera:FindFirstChild("Beam")
if Beam then Beam:Destroy() end

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

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

Thank you, but I am trying to make it where when the local player dies the beam resets and starts spinning slowly and then eventually gets faster. And this repeats over and over again as soon as the player dies. If what I’m explaining is too complicated please ask me to try and reexplain.

That is what the script I posted will do. Did you try it?

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()
        Beam.CFrame = CFrame.new(0,0,0)
		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
   Move()
end

Screen Recording (9-26-2022 7-37-33 PM).wmv (8.6 MB)

How do you add a screen capture, in the comments where it just ‘shows’ instead of people having to download it?

1 Like

I’m not sure but I did download the video and watched it through. What you did in the video was exactly what I wanted to happen. For some reason what is happening for you isn’t happening for me. When I test play the game the beam isn’t being cloned.

Here is what im using -
image

image

local Beam = workspace.CurrentCamera:FindFirstChild("Beam")
if Beam then Beam:Destroy() end

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

local speed = 0.001
while script.Parent and script.Parent.Parent do
	task.wait()
	Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
	speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
end

make sure you are using a local script , not a script

In my example I was using a script, but under the script properties, I had this set to client…
image

But its probably easier if you just use a localscript

Thanks so much, I was really struggling with this. One last question if possible. Is there anyway I can trigger this with a RemoteEvent?

For example, would something like this work?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Remotes:WaitForChild("Spinner")

local Beam = workspace.CurrentCamera:FindFirstChild("Beam")
if Beam then Beam:Destroy() end

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

local function spin()
local speed = 0.001
while script.Parent and script.Parent.Parent do
	task.wait()
	Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
	speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
	end
	end

remoteEvent.OnClientEvent:Connect(spin)

Yes that should work, just you wouldn’t be able to stop it though.

1 Like

Maybe something like this if you need more control, such as stopping and starting

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Remotes:WaitForChild("Spinner")

local Beam = workspace.CurrentCamera:FindFirstChild("Beam")
if Beam then Beam:Destroy() end

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

local defaultCFrame = Beam.CFrame
local speed = 0.001
local stopped = true

function DoSpin()
	while script.Parent and script.Parent.Parent do
		task.wait()
		if not stopped then
			Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
			speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
		end
	end
end

remoteEvent.OnClientEvent:Connect(function(message)
	message = message or "Start"
	if message == "Start" then
		stopped = false
	else
		stopped = true
	end
end)

DoSpin()
1 Like

Hm, this didn’t seem to work. This is still supposed to be a local script in StarterCharacterScripts right? What happens is when the function is called and the player dies the beam just stops. It stops and it doesn’t start rotating again.

So if its spinning and you reset or die, did you want it to remain in the last state it was in?
Did you want it to maintain the same speed, or reset the speed?

Not sure exactly what you wanted it to do, but was just suggesting a way you could turn it on and off.

In the code below, we set an attribute on the Beam to state if it is Stopped or not.
So if you reset, it will remember if it is Stopped, but the speed will reset and start out slow again, it will also set its starting position to the default.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Remotes:WaitForChild("Spinner")

local Beam = workspace.CurrentCamera:FindFirstChild("Beam")
if not Beam then
	Beam = ReplicatedStorage:WaitForChild("Beam"):Clone()
	Beam.Parent = workspace.CurrentCamera
	Beam:SetAttribute("Stopped",true)	
end

Beam.CFrame = ReplicatedStorage:WaitForChild("Beam").CFrame

local speed = 0.001

function DoSpin()
	while script.Parent and script.Parent.Parent do
		task.wait()
		if not Beam:GetAttribute("Stopped") then
			Beam.CFrame = Beam.CFrame * CFrame.fromEulerAnglesXYZ(speed, 0, 0)
			speed = math.clamp(speed + (0.001 / 90), 0.001, 30)
		end
	end
end

remoteEvent.OnClientEvent:Connect(function(message)
	message = message or "Start"
	if message == "Start" then
		Beam:SetAttribute("Stopped",false)	
	else
		Beam:SetAttribute("Stopped",true)	
	end
end)

DoSpin()

1 Like

Thanks, this is exactly what I meant. Appreciate it a lot that you went on helping me. :slight_smile:

1 Like