Script stops working after respawning

i actually have no idea why this does not work after respawning. i am clueless
please help, this happens when respawning:


what is supposed to happen:

(dont mind the menu)

local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local contentProvider = game:GetService("ContentProvider")

local player = game.Players.LocalPlayer
local gui = player.PlayerGui:WaitForChild("FirstPerson")
local character = player.Character or player.CharacterAdded:Wait()
local currentCamera = game.Workspace.CurrentCamera
local changedpersonview = false
local root = character:WaitForChild("HumanoidRootPart")
local neck = character:FindFirstChild("Neck", true)
local head = character:WaitForChild("Head")
local yOffset = neck.C0.Y
local died = false

head.CanCollide = true

local HeadCam = Instance.new("Part")
HeadCam.Parent = head
HeadCam.Name = "HeadCameraPart"
HeadCam.Size = Vector3.new(0.5,0.5,0.5)
HeadCam.CFrame = head.CFrame + Vector3.new(0, 0.3, -0.4)
HeadCam.Transparency = 1

local HeadCamWeld = Instance.new("WeldConstraint")
HeadCamWeld.Parent = HeadCam
HeadCamWeld.Name = "HeadCameraPartWeld"
HeadCamWeld.Part0 = HeadCam
HeadCamWeld.Part1 = head

currentCamera.FieldOfView = 70
currentCamera.CameraSubject = HeadCam
local defaultcframe = currentCamera.CFrame
currentCamera.CFrame = HeadCam.CFrame

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftAlt then
			gui.MouseLock.Modal = true
		end
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.LeftAlt then
			gui.MouseLock.Modal = false
		end
	end
end)

local CFNew, CFAng, asin = CFrame.new, CFrame.Angles, math.asin

if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
	contentProvider:PreloadAsync(character:GetDescendants())

	for _, child in pairs(character:GetDescendants()) do
		if child:IsA("Accessory") then
			for _, accessory in pairs(child:GetChildren()) do
				if accessory:IsA("MeshPart") then
					accessory.Transparency = 1
				end
			end
		elseif child.Name == "Head" and child:IsA("MeshPart") then
			child.Transparency = 1
		end
	end

	character.DescendantAdded:Connect(function(child)
		wait()
		if child:IsA("Accessory") then
			for _, accessory in pairs(child:GetChildren()) do
				if accessory:IsA("MeshPart") then
					accessory.Transparency = 1
				end
			end
		end
	end)

	runService.RenderStepped:Connect(function()
		if not died then
			local x1, y1, z1 = currentCamera.CFrame:ToOrientation()
			local x2, y2, z2 = character.PrimaryPart.CFrame:ToOrientation()

			character.PrimaryPart.CFrame *= CFrame.Angles(0, y1 - y2, 0)
			if neck then
				local camDirection = root.CFrame:toObjectSpace(currentCamera.CFrame).lookVector
				local neckBefore = neck.C0

				neck.C0 = CFNew(0, yOffset, 0) * CFAng(asin(camDirection.Y), -asin(camDirection.X), 0)

				if neck.C0 ~= neckBefore then
					game.ReplicatedStorage:WaitForChild("NeckEvent"):FireServer(neck.C0)
				end
			end
		end
	end)
elseif character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
	contentProvider:PreloadAsync(character:GetDescendants())

	for _, child in pairs(character:GetDescendants()) do
		if child:IsA("Accessory") then
			for _, accessory in pairs(child:GetChildren()) do
				if accessory:IsA("Part") or accessory:IsA("MeshPart") then
					accessory.Transparency = 1
				end
			end
		elseif child.Name == "Head" and child:IsA("Part") then
			child.Transparency = 1
		end
	end

	character.DescendantAdded:Connect(function(child)
		wait()
		if child:IsA("Accessory") then
			for _, accessory in pairs(child:GetChildren()) do
				if accessory:IsA("Part") or accessory:IsA("MeshPart") then
					accessory.Transparency = 1
				end
			end
		end
	end)

	character.PrimaryPart = root
	head.Size = Vector3.new(1,1,1)

	neck.Parent = head
	neck.Part0 = character.Torso
	neck.Part1 = character.Head
	neck.DesiredAngle = 0
	neck.MaxVelocity = 0
	task.wait()

	runService.RenderStepped:Connect(function()
		if not died and (currentCamera.Focus.p - currentCamera.CFrame.p).magnitude <= 1 then
			if changedpersonview == false then
				currentCamera.CameraSubject = HeadCam
				currentCamera.CFrame = HeadCam.CFrame
				changedpersonview = true
			end
			

			for i, v in pairs(character:GetChildren()) do
				if v:IsA("Accessory") then
					for _, accessory in pairs(v:GetChildren()) do
						if accessory:IsA("Part") or accessory:IsA("MeshPart") then
							accessory.Transparency = 1
						end
					end
				end 
			end
			character.Head.Transparency = 1
			local x1, y1, z1 = currentCamera.CFrame:ToOrientation()
			local x2, y2, z2 = character.PrimaryPart.CFrame:ToOrientation()

			character.PrimaryPart.CFrame *= CFrame.Angles(0, y1 - y2, 0)
			if neck then
				local camDirection = root.CFrame:toObjectSpace(currentCamera.CFrame).lookVector
				local neckBefore = neck.C0

				neck.C0 = CFNew(0, yOffset, 0) * CFAng(asin(camDirection.Y) - math.rad(90), 0, -asin(camDirection.X) + math.rad(180))

				if neck.C0 ~= neckBefore then
					game.ReplicatedStorage:WaitForChild("NeckEvent"):FireServer(neck.C0)
				end
			end
		else
			neck.C0 = CFNew(0, yOffset, 0) * CFAng(math.rad(90),math.rad(180),0)
			for i, v in pairs(character:GetChildren()) do
				if v:IsA("Accessory") then
					for _, accessory in pairs(v:GetChildren()) do
						if accessory:IsA("Part") or accessory:IsA("MeshPart") then
							accessory.Transparency = 0
						end
					end
				end 
			end
			character.Head.Transparency = 0
			currentCamera.CameraSubject = character.Humanoid
			changedpersonview = false
			
		end
	end)
end

game.ReplicatedStorage:WaitForChild("NeckEvent").OnClientEvent:Connect(function(sender, neckCFrame)
	local senderNeck = sender.character:FindFirstChild("Neck", true)

	if senderNeck then
		senderNeck.C0 = neckCFrame
	end
end)

character.Humanoid.Died:Connect(function()
	died = true
end)

player.CharacterAdded:Connect(function()
	died = false
end)

(updated script for easier testing)

1 Like
player.CharacterAdded:Connect(function()
	died = true
end)
2 Likes

ive already tried that, reason why i disabled it was cause it didnt work

probably won’t work but this is the function that is fired when the character dies; not the character added function

player.CharacterRemoving:Connect(function()
	died = true
end)
1 Like

i have put character.Humanoid.Died but didnt work

hmm I have no idea what the problem is, the only thing I could suggest is trying to print the CFrame

1 Like

is that the problem? it gives me a cframe error when i die but i thought localscripts reset when character gets added?

ive fixed it by putting the script into starterplayerscripts and changing the code a bit, thanks everyone for helping

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.