Help with my Camera and Control script(doesnt work after death)

i need help on my camera script and control script semems like when you die the script doesnt work and it doesnt have any error both the control and camera goes broken when you die

ControlScript
local movements = {
	["forw"] = Enum.KeyCode.W,
	["left"] = Enum.KeyCode.A,
	["back"] = Enum.KeyCode.S,
	["righ"] = Enum.KeyCode.D,
}

local inputs = setmetatable({}, {
	__index = function(t, k)
		return game:GetService("UserInputService"):IsKeyDown(movements[k]) and 1 or 0
	end
})
local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local con = game:GetService("RunService").RenderStepped:Connect(function()
	if char and char:FindFirstChild("HumanoidRootPart") then
		local movez = inputs.forw - inputs.back
		local movex = inputs.righ - inputs.left
		script.Parent.X.Value = movex -- thsese are for my car to move around
		script.Parent.Z.Value = movez -- thsese are for my car to move around
player:Move(Vector3.new(0, 0, -movez) + Vector3.new(movex, 0, 0), true)		
	else
		
	end
end)

--[[humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
	if humanoid.Sit == true then
		con:Disconnect()
	else
		local con = game:GetService("RunService").RenderStepped:Connect(function()
			local movez = inputs.forw - inputs.back
			local movex = inputs.righ - inputs.left
			player:Move(Vector3.new(0, 0, -movez) + Vector3.new(movex, 0, 0), true)
		end)
	end
end)]]

if uis.KeyboardEnabled and not uis.TouchEnabled then
	local deb = false
	uis.InputBegan:Connect(function(i,g)
		if i.KeyCode == Enum.KeyCode.Space and not g then
			if not deb then
				deb = true
				humanoid.Jump = true
				wait(.1)
				humanoid.Jump = false
				wait(.5)
				deb = false
			end
		end
	end)
else
	local deb = false
	player:WaitForChild("PlayerGui"):WaitForChild("Menu"):WaitForChild("JumpButton").MouseButton1Click:Connect(function()
		deb = true
		humanoid.Jump = true
		wait(.1)
		humanoid.Jump = false
		wait(.5)
		deb = false
	end)
	player.CharacterAdded:Connect(function()
		deb = false
		player:WaitForChild("PlayerGui"):WaitForChild("Menu"):WaitForChild("JumpButton").MouseButton1Click:Connect(function()
			deb = true
			humanoid.Jump = true
			wait(.1)
			humanoid.Jump = false
			wait(.5)
			deb = false
		end)
	end)
end
CameraScript
-------------
--- INDEX ---
-------------

--Services
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

--Player & Character
local localPlayer = game:GetService("Players").LocalPlayer

--Other
local Camera = game:GetService("Workspace").CurrentCamera

--------------
--- VALUES ---
--------------
local Character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local xAngle = 0
local yAngle = 0
local cameraPos = Vector3.new(3,0,7.5)

----------------------
--- INITIALIZATION ---
----------------------

wait(1)

-----------------
--- FUNCTIONS ---
-----------------

userInputService.InputChanged:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseMovement then
		xAngle = xAngle-input.Delta.x*0.4
		--Clamp the vertical axis so it doesn't go upside down or glitch.
		yAngle = math.clamp(yAngle-input.Delta.y*0.4,-80,80)
	end
end)

local con = runService.RenderStepped:Connect(function()
	userInputService.MouseIconEnabled = false
	Camera.FieldOfView = 85
	if Character and Character:FindFirstChild("HumanoidRootPart") then
		Camera.CameraType = Enum.CameraType.Scriptable
		userInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		local rootPart = Character:WaitForChild("HumanoidRootPart")
		--Set rotated start cframe inside head
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)

		--Set camera focus and cframe
		local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
		local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

		Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
	else
		Camera.CameraType = Enum.CameraType.Custom
		Camera.CameraSubject = Character:FindFirstChild("Humanoid")
	end
end)
Character:WaitForChild("Humanoid"):GetPropertyChangedSignal("Sit"):Connect(function()
	if Character:WaitForChild("Humanoid").Sit == true then
		local car = false
		local seatpart
		repeat wait()
			seatpart = Character:WaitForChild("Humanoid").SeatPart
		until Character:WaitForChild("Humanoid").SeatPart ~= nil
		for i,v in pairs(seatpart.Parent:GetAttributes()) do
			if i == "Car" then
				car = true
			end
		end
		if car then
			cameraPos = Vector3.new(0,0,Character:WaitForChild("Humanoid").SeatPart.Parent.Body.Body.Size.Z)
		end
	else
		cameraPos = Vector3.new(3,0,7.5)
	end
end)

thx for the help!

1 Like

You reference old character/humanoid variables, even after they die. Not only is this a memory leak, but this is causing your problem.

oh really if then how would i fix it?

im new to making custom control and camera script

When a new character spawns, re-set those varables.

local char = player.Character
if not char or not char.Parent then
   char = player.CharacterAdded:Wait()
end

This should do it.

that did not work sadly…
:frowning: