Localscript swon't run when disabled then reenabled

I have this localscript that for some reason yields infinitely when i disable it and reenable it, How can I prevent this from happening.

1 Like

Where did you put the local script and can you show the script?

1 Like

image

--Get service needed for events used in this script
local RunService = game:GetService("RunService")	

-- Variables for the camera and player
local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
player.CharacterAdded:Wait()
-- Constant variable used to set the camera’s offset from the player
local CAMERA_OFFSET = player.Character:WaitForChild("CameraOffset").Value

camera.CameraType = Enum.CameraType.Scriptable

local debounce = false
local function onRenderStep()
	print("lol")
	local success , err = pcall(function()

		if debounce == false then
			debounce = true

			if player.Character:FindFirstChild("HumanoidRootPart") and player.Character:FindFirstChild("CameraOffset") then
				if camera.CameraType == Enum.CameraType.Scriptable then

					local CAMERA_OFFSET = player.Character.CameraOffset.Value
					local playerPosition = player.Character.HumanoidRootPart.Position
					local cameraPosition = playerPosition + CAMERA_OFFSET

					-- make the camera follow the player
					camera.CoordinateFrame = CFrame.new(cameraPosition, playerPosition)
					camera.CFrame = CFrame.fromMatrix(
						cameraPosition,
						Vector3.new(1,0,0),
						Vector3.new(0,0,-1)
					)
				end
			end
			debounce = false
		end
	end)
	if not success then warn(err) end
end
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)

I remember seeing the post about it yeilding indefinitely. I believe this is beciase of the player.CharacterAdded:Wait(). It is waiting for the characterAdded event to fire but if the character is already there when you reenable the script then it wont fire until the character dies and loads in again.