Localscript won'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.

If it yields indefinitely it probably has something to do with the script itself. You would need to provide it to debug.


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

refer to the reply I just made to the other post^^

1 Like

I removed every waitforchild and it still does not run or retrn an error after being disabled and then not.

Where are you re enabling the script. Does putting a print at the top of the script print anything?

The script is disabled and enabled through a localscript, no.

When it runs the first time does it work?

yes it does.
:shallow_pan_of_food:

And you removed the player.CharacterAdded:Wait()?? and it doesnt print anything when you put a print at the first line???

nope
:shallow_pan_of_food: :shallow_pan_of_food:

send the edited script and the script that disables and enables it

Cmera script:

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

script.MatrixValue.Value = CFrame.fromMatrix(
	Vector3.new(0,0,0),
	Vector3.new(1,0,0),
	Vector3.new(0,0,-1)
)

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

					if script.UseMatrix.Value == true then

						-- make the camera follow the player
						camera.CFrame = CFrame.fromMatrix(
							cameraPosition,
							script.MatrixValue.Value.UpVector,
							script.MatrixValue.Value.RightVector
						)
						
					else
						camera.CFrame = CFrame.new(cameraPosition , playerPosition)
					end
				end
			end
			debounce = false
		end
	end)
	if not success then warn(err) end
end
RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onRenderStep)

Script that disables (“Now just changes camera type but pretend it’s not”):

local cs = game:GetService("CollectionService")

wait(3)
game:GetService("TweenService"):Create(game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame , TweenInfo.new(0.2) , {BackgroundTransparency = 1}):Play()
local Parts = cs:GetTagged("DeathRegion")

local functions = {}
local db = false


local function PseudoDeath(Otherpa)

	if db == false then
		db = true

		if game:GetService("Players"):GetPlayerFromCharacter(Otherpa.Parent) == game.Players.LocalPlayer then

			local player = game:GetService("Players"):GetPlayerFromCharacter(Otherpa.Parent)
			
			player.Character.CameraOffset:Destroy()
			wait(1)
			game:GetService("TweenService"):Create(game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame , TweenInfo.new(0.2) , {BackgroundTransparency = 0}):Play()
			wait(1)
			game.ReplicatedStorage.RespawnCharacter:FireServer()
			player.CharacterAdded:Wait()
			game:GetService("TweenService"):Create(game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").Frame , TweenInfo.new(0.2) , {BackgroundTransparency = 1}):Play()
			


		end
		db = false
	end
end

for i , v in pairs(Parts) do
	v.Touched:Connect(PseudoDeath)
end

You still have player.CharacterAdded:Wait() in both of them.?

Oop that was the problem my bad .-.