Character Child added doesn't work after a death

i was making a gun script but i found a glitch where if you die, childAdded function just stops working completely.
I have never gotten this issue before, so i don’t really know how to fix it.

checked all over the internet but still haven’t found any solutions yet.

FrameWork:

--//Services
local User_Input_Service = game:GetService("UserInputService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Player_Service = game:GetService("Players")
local Run_Service = game:GetService("RunService")

--//Player
local Player = Player_Service.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local Current_Camera = workspace.CurrentCamera

local Mouse = Player:GetMouse()

--//Variables
local Aim_CFrame = CFrame.new()

local Sway_CFrame = CFrame.new()
local Last_Camera_CF = CFrame.new()

--//Values
local View_Model_Value = nil

local Is_Aiming = false

local Bobble_Speed = 0
local Sway_Amount = 0.5

--| Main Script |--
local Calculate_Distance = function(ObjectA, ObjectB)
	local Distance = (ObjectA - ObjectB).Magnitude
	return Distance
end

local Destroy_ViewModel = function(Tool)
	print("Destroyed")
	if Tool and Tool:IsA("Tool") then
		User_Input_Service.MouseIconEnabled = true
		if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name); ViewModel:Destroy()
			View_Model_Value = nil
		end
	end
end

local Position_ViewModel = function(ViewModel)
	if ViewModel and Humanoid.Health > 0 then
		if ViewModel:FindFirstChild("Right Arm") and ViewModel:FindFirstChild("Left Arm") then
			ViewModel["Right Arm"].CanCollide = false
			ViewModel["Left Arm"].CanCollide = false
		end
		
		local Rotation = Current_Camera.CFrame:ToObjectSpace(Last_Camera_CF)
		local X, Y, Z = Rotation:ToOrientation()
		Sway_CFrame = Sway_CFrame:Lerp(CFrame.Angles(math.sin(X) * Sway_Amount, math.sin(Y) * Sway_Amount, 0), 0.1)
		Last_Camera_CF = Current_Camera.CFrame

		if Humanoid.WalkSpeed > 16 then Bobble_Speed = 11.5
		else Bobble_Speed = 6.5 end
		
		local Bobble_Offset = CFrame.new()
		if Humanoid.MoveDirection ~= Vector3.new() then
			if not Is_Aiming then
				local Cos = math.cos(tick() * Bobble_Speed * 2) * 1
				local Sine = math.sin(tick() * Bobble_Speed) * 2
				local ZAxis = -math.sin(tick() * Bobble_Speed) * 4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis))
			else
				local Cos = math.cos(tick() * Bobble_Speed) * 0.1
				local Sine = math.sin(tick() * Bobble_Speed / 2) * 0.2
				local ZAxis = -math.sin(tick() * Bobble_Speed/ 2) * 0.4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(0))
				end
		else
			local Cos = math.cos(tick() * Bobble_Speed / 5) * 0.1
			local Sine = math.sin(tick() * Bobble_Speed / 7) * 0.4
			local ZAxis = -math.sin(tick() * Bobble_Speed / 7) * 0.8
			Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis)) 
			end
		
	if ViewModel.PrimaryPart then
		ViewModel:SetPrimaryPartCFrame(Current_Camera.CFrame * Sway_CFrame * Aim_CFrame * Bobble_Offset)
	end
		if Is_Aiming then
			if ViewModel:FindFirstChild("AimCF") then
				local Offset = ViewModel:WaitForChild("AimCF").CFrame:ToObjectSpace(ViewModel.PrimaryPart.CFrame)
				Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.2)
				end
		else
			local Offset = CFrame.new()
			Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.1)
		end
		end
end

-- CHILD ADDED LINE ---
local Load_View_Model = function(Tool)
	if Tool:IsA("Tool") and Humanoid.Health > 0 and Calculate_Distance(Head.Position, Current_Camera.CFrame.p) < 1 then
		User_Input_Service.MouseIconEnabled = false
		if Replicated_Storage.ViewModels:FindFirstChild(Tool.Name) then
			local Choosen_Model = Replicated_Storage.ViewModels:FindFirstChild(Tool.Name):Clone()
			if Character:FindFirstChildOfClass("Shirt") then
				local Shirt = Character.Shirt:Clone(); Shirt.Parent = Choosen_Model
				print("Added Shirt")
			end
			if Character:FindFirstChild("Body Colors") then
				local Colors = Character["Body Colors"]:Clone(); Colors.Parent = Choosen_Model
				print("Added Colors")
			end
			Choosen_Model.Parent = Current_Camera
			Run_Service.RenderStepped:Connect(function()
				if Tool.Parent == Character then
					View_Model_Value = Choosen_Model
					View_Model_Value = tostring(View_Model_Value.Name)
					Position_ViewModel(Choosen_Model)		
					end
			end)
		else
			print("Didnt find tool viewmodel")
		end
	else
		print("Not a tool")
	end
end

local Tool_Invisibility = function()
	if Character:FindFirstChildWhichIsA("Tool") then
	local Tool = Character:FindFirstChildWhichIsA("Tool")
			if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name)
			local Distance = Calculate_Distance(Head.Position, Current_Camera.CFrame.p)
			if Distance < 1 then
				for _, Part in ipairs(Tool:GetChildren()) do
					if Distance < 0.8 then
							if Part then
								Part.Transparency = 1
						end
					else
						if Part then
								Part.Transparency = 0
								Destroy_ViewModel(Tool)
						end
						end
				end
			end
		end
	end
end

User_Input_Service.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = true
				end
end)

User_Input_Service.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = false
	end
end)

Humanoid.Died:Connect(function(died)
	if View_Model_Value then
		Destroy_ViewModel(View_Model_Value)
		end
end)


Run_Service.RenderStepped:Connect(Tool_Invisibility)

-- HERE CHILD ADDED FUNCTIONS RUNS --
Character.ChildAdded:Connect(Load_View_Model)
Character.ChildRemoved:Connect(Destroy_ViewModel)

this isn’t a glitch when you die your character is destroyed and a new one is created in the workspace when you respawn

When the character dies, it is destroyed and a new one is created in its place.

To fix your issue, simply add a CharacterAdded connection and update the necessary variables.

local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
-- etc, etc

local function characterAdded(newChar)
    char = newChar
    hum = char.Humanoid
    -- etc, etc
end

player.CharacterAdded:Connect(characterAdded)

Edit: Clarity

When i add CharacterAdded function, Script just stops from working completely,
it doesn’t print “Child Added”, It doesn’t detect if child gets added:
Script:

--//Services
local User_Input_Service = game:GetService("UserInputService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Player_Service = game:GetService("Players")
local Run_Service = game:GetService("RunService")

--//Player
local Player = Player_Service.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local Current_Camera = workspace.CurrentCamera

local Mouse = Player:GetMouse()

--//Variables
local Aim_CFrame = CFrame.new()

local Sway_CFrame = CFrame.new()
local Last_Camera_CF = CFrame.new()

--//Values
local View_Model_Value = nil

local Is_Aiming = false

local Bobble_Speed = 0
local Sway_Amount = 0.5

--| Main Script |--
local Calculate_Distance = function(ObjectA, ObjectB)
	local Distance = (ObjectA - ObjectB).Magnitude
	return Distance
end

local Destroy_ViewModel = function(Tool)
	print("Destroyed")
	if Tool and Tool:IsA("Tool") then
		User_Input_Service.MouseIconEnabled = true
		if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name); ViewModel:Destroy()
			View_Model_Value = nil
		end
	end
end

local Position_ViewModel = function(ViewModel)
	if ViewModel and Humanoid.Health > 0 then
		if ViewModel:FindFirstChild("Right Arm") and ViewModel:FindFirstChild("Left Arm") then
			ViewModel["Right Arm"].CanCollide = false
			ViewModel["Left Arm"].CanCollide = false
		end
		
		local Rotation = Current_Camera.CFrame:ToObjectSpace(Last_Camera_CF)
		local X, Y, Z = Rotation:ToOrientation()
		Sway_CFrame = Sway_CFrame:Lerp(CFrame.Angles(math.sin(X) * Sway_Amount, math.sin(Y) * Sway_Amount, 0), 0.1)
		Last_Camera_CF = Current_Camera.CFrame

		if Humanoid.WalkSpeed > 16 then Bobble_Speed = 11.5
		else Bobble_Speed = 6.5 end
		
		local Bobble_Offset = CFrame.new()
		if Humanoid.MoveDirection ~= Vector3.new() then
			if not Is_Aiming then
				local Cos = math.cos(tick() * Bobble_Speed * 2) * 1
				local Sine = math.sin(tick() * Bobble_Speed) * 2
				local ZAxis = -math.sin(tick() * Bobble_Speed) * 4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis))
			else
				local Cos = math.cos(tick() * Bobble_Speed) * 0.1
				local Sine = math.sin(tick() * Bobble_Speed / 2) * 0.2
				local ZAxis = -math.sin(tick() * Bobble_Speed/ 2) * 0.4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(0))
				end
		else
			local Cos = math.cos(tick() * Bobble_Speed / 5) * 0.1
			local Sine = math.sin(tick() * Bobble_Speed / 7) * 0.4
			local ZAxis = -math.sin(tick() * Bobble_Speed / 7) * 0.8
			Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis)) 
			end
		
	if ViewModel.PrimaryPart then
		ViewModel:SetPrimaryPartCFrame(Current_Camera.CFrame * Sway_CFrame * Aim_CFrame * Bobble_Offset)
	end
		if Is_Aiming then
			if ViewModel:FindFirstChild("AimCF") then
				local Offset = ViewModel:WaitForChild("AimCF").CFrame:ToObjectSpace(ViewModel.PrimaryPart.CFrame)
				Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.2)
				end
		else
			local Offset = CFrame.new()
			Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.1)
		end
		end
end

local Load_View_Model = function(Tool, NewCharacter)
	print("Child Added")
	if Tool:IsA("Tool") and NewCharacter.Humanoid.Health > 0 and Calculate_Distance(NewCharacter.Head.Position, Current_Camera.CFrame.p) < 1 then
		User_Input_Service.MouseIconEnabled = false
		if Replicated_Storage.ViewModels:FindFirstChild(Tool.Name) then
			local Choosen_Model = Replicated_Storage.ViewModels:FindFirstChild(Tool.Name):Clone()
			if NewCharacter:FindFirstChildOfClass("Shirt") then
				local Shirt = NewCharacter.Shirt:Clone(); Shirt.Parent = Choosen_Model
				print("Added Shirt")
			end
			if NewCharacter:FindFirstChild("Body Colors") then
				local Colors = NewCharacter["Body Colors"]:Clone(); Colors.Parent = Choosen_Model
				print("Added Colors")
			end
			Choosen_Model.Parent = Current_Camera
			Run_Service.RenderStepped:Connect(function()
				if Tool.Parent == NewCharacter then
					View_Model_Value = Choosen_Model
					View_Model_Value = tostring(View_Model_Value.Name)
					Position_ViewModel(Choosen_Model)		
					end
			end)
		else
			print("Didnt find tool viewmodel")
		end
	else
		print("Not a tool")
	end
end

local Tool_Invisibility = function()
	if Character:FindFirstChildWhichIsA("Tool") then
	local Tool = Character:FindFirstChildWhichIsA("Tool")
			if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name)
			local Distance = Calculate_Distance(Head.Position, Current_Camera.CFrame.p)
			if Distance < 1 then
				for _, Part in ipairs(Tool:GetChildren()) do
					if Distance < 0.8 then
							if Part then
								Part.Transparency = 1
						end
					else
						if Part then
								Part.Transparency = 0
								Destroy_ViewModel(Tool)
						end
						end
				end
			end
		end
	end
end

local Character_Added = function(NewCharacter)
	NewCharacter.ChildAdded:Connect(Load_View_Model, NewCharacter)
end

User_Input_Service.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = true
				end
end)

User_Input_Service.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = false
	end
end)

Humanoid.Died:Connect(function(died)
	if View_Model_Value then
		Destroy_ViewModel(View_Model_Value)
		end
end)


Run_Service.RenderStepped:Connect(Tool_Invisibility)

--Character.ChildAdded:Connect(Load_View_Model)
Character.ChildRemoved:Connect(Destroy_ViewModel)

Oh, right, I forgot you would have to re-connect all those connections.
To make this easier, I would wrap all these other functions and connections into a function, call it once, and call it everytime CharacterAdded is fired.

local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid
-- etc, etc

local function init()
    -- Everything else goes in here
    char.ChildAdded:Connect(function)
    -- etc, etc
end
init() -- Call it once for our already-existing variables

local function characterAdded(newChar)
    char = newChar
    hum = char.Humanoid
    -- etc, etc
    
    init() -- Call the init function, reinstate all those connections
end

player.CharacterAdded:Connect(characterAdded)

it didn’t work, the only thing it did is create an issue with Viewmodel deletion.

--//Services
local User_Input_Service = game:GetService("UserInputService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Player_Service = game:GetService("Players")
local Run_Service = game:GetService("RunService")

--//Player
local Player = Player_Service.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local Current_Camera = workspace.CurrentCamera

local Mouse = Player:GetMouse()

--//Variables
local Aim_CFrame = CFrame.new()

local Sway_CFrame = CFrame.new()
local Last_Camera_CF = CFrame.new()

--//Values
local View_Model_Value = nil

local Is_Aiming = false

local Bobble_Speed = 0
local Sway_Amount = 0.5

--| Main Script |--
local Calculate_Distance = function(ObjectA, ObjectB)
	local Distance = (ObjectA - ObjectB).Magnitude
	return Distance
end

local Destroy_ViewModel = function(Tool)
	print("Destroyed")
	if Tool and Tool:IsA("Tool") then
		User_Input_Service.MouseIconEnabled = true
		if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name); ViewModel:Destroy()
			View_Model_Value = nil
		end
	end
end

local Position_ViewModel = function(ViewModel)
	if ViewModel and Humanoid.Health > 0 then
		if ViewModel:FindFirstChild("Right Arm") and ViewModel:FindFirstChild("Left Arm") then
			ViewModel["Right Arm"].CanCollide = false
			ViewModel["Left Arm"].CanCollide = false
		end
		
		local Rotation = Current_Camera.CFrame:ToObjectSpace(Last_Camera_CF)
		local X, Y, Z = Rotation:ToOrientation()
		Sway_CFrame = Sway_CFrame:Lerp(CFrame.Angles(math.sin(X) * Sway_Amount, math.sin(Y) * Sway_Amount, 0), 0.1)
		Last_Camera_CF = Current_Camera.CFrame

		if Humanoid.WalkSpeed > 16 then Bobble_Speed = 11.5
		else Bobble_Speed = 6.5 end
		
		local Bobble_Offset = CFrame.new()
		if Humanoid.MoveDirection ~= Vector3.new() then
			if not Is_Aiming then
				local Cos = math.cos(tick() * Bobble_Speed * 2) * 1
				local Sine = math.sin(tick() * Bobble_Speed) * 2
				local ZAxis = -math.sin(tick() * Bobble_Speed) * 4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis))
			else
				local Cos = math.cos(tick() * Bobble_Speed) * 0.1
				local Sine = math.sin(tick() * Bobble_Speed / 2) * 0.2
				local ZAxis = -math.sin(tick() * Bobble_Speed/ 2) * 0.4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(0))
				end
		else
			local Cos = math.cos(tick() * Bobble_Speed / 5) * 0.1
			local Sine = math.sin(tick() * Bobble_Speed / 7) * 0.4
			local ZAxis = -math.sin(tick() * Bobble_Speed / 7) * 0.8
			Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis)) 
			end
		
	if ViewModel.PrimaryPart then
		ViewModel:SetPrimaryPartCFrame(Current_Camera.CFrame * Sway_CFrame * Aim_CFrame * Bobble_Offset)
	end
		if Is_Aiming then
			if ViewModel:FindFirstChild("AimCF") then
				local Offset = ViewModel:WaitForChild("AimCF").CFrame:ToObjectSpace(ViewModel.PrimaryPart.CFrame)
				Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.2)
				end
		else
			local Offset = CFrame.new()
			Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.1)
		end
		end
end

local Load_View_Model = function(Tool, NewCharacter)
	print("Child Added")
	if Tool:IsA("Tool") and NewCharacter.Humanoid.Health > 0 and Calculate_Distance(NewCharacter.Head.Position, Current_Camera.CFrame.p) < 1 then
		User_Input_Service.MouseIconEnabled = false
		if Replicated_Storage.ViewModels:FindFirstChild(Tool.Name) then
			local Choosen_Model = Replicated_Storage.ViewModels:FindFirstChild(Tool.Name):Clone()
			if NewCharacter:FindFirstChildOfClass("Shirt") then
				local Shirt = NewCharacter.Shirt:Clone(); Shirt.Parent = Choosen_Model
				print("Added Shirt")
			end
			if NewCharacter:FindFirstChild("Body Colors") then
				local Colors = NewCharacter["Body Colors"]:Clone(); Colors.Parent = Choosen_Model
				print("Added Colors")
			end
			Choosen_Model.Parent = Current_Camera
			Run_Service.RenderStepped:Connect(function()
				if Tool.Parent == NewCharacter then
					View_Model_Value = Choosen_Model
					View_Model_Value = tostring(View_Model_Value.Name)
					Position_ViewModel(Choosen_Model)		
					end
			end)
		else
			print("Didnt find tool viewmodel")
		end
	else
		print("Not a tool")
	end
end

local Tool_Invisibility = function()
	if Character:FindFirstChildWhichIsA("Tool") then
	local Tool = Character:FindFirstChildWhichIsA("Tool")
			if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name)
			local Distance = Calculate_Distance(Head.Position, Current_Camera.CFrame.p)
			if Distance < 1 then
				for _, Part in ipairs(Tool:GetChildren()) do
					if Distance < 0.8 then
							if Part then
								Part.Transparency = 1
						end
					else
						if Part then
								Part.Transparency = 0
								Destroy_ViewModel(Tool)
						end
						end
				end
			end
		end
	end
end


Character.ChildAdded:Connect(function(Child)
	Load_View_Model(Child, Character)
end)

User_Input_Service.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = true
				end
end)

User_Input_Service.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
			Is_Aiming = false
	end
end)

Humanoid.Died:Connect(function(died)
	if View_Model_Value then
		Destroy_ViewModel(View_Model_Value)
		end
end)


Run_Service.RenderStepped:Connect(Tool_Invisibility)

Player.CharacterAdded:Connect(function(NewCharacter)
	Character.ChildAdded:Connect(function(Child)
		Load_View_Model(Child, NewCharacter)
	end)
	Character.ChildRemoved:Connect(Destroy_ViewModel)
	end)

You aren’t changing your character-related variables at all, and connections like this one

aren’t being re-connected either.

I think you’re misunderstanding my replies.

I’ll cook up a replacement script, give me a second.

oh sorry for misunderstanding.

No worries, you’re fine.

Hopefully this works?

--//Services
local User_Input_Service = game:GetService("UserInputService")
local Replicated_Storage = game:GetService("ReplicatedStorage")
local Player_Service = game:GetService("Players")
local Run_Service = game:GetService("RunService")

--//Player
local Player = Player_Service.LocalPlayer
local Character = Player.CharacterAdded:Wait() or Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local Current_Camera = workspace.CurrentCamera

local Mouse = Player:GetMouse()

--//Variables
local Aim_CFrame = CFrame.new()

local Sway_CFrame = CFrame.new()
local Last_Camera_CF = CFrame.new()

--//Values
local View_Model_Value = nil

local Is_Aiming = false

local Bobble_Speed = 0
local Sway_Amount = 0.5

--| Main Script |--
local Calculate_Distance = function(ObjectA, ObjectB)
	local Distance = (ObjectA - ObjectB).Magnitude
	return Distance
end

local Destroy_ViewModel = function(Tool)
	print("Destroyed")
	if Tool and Tool:IsA("Tool") then
		User_Input_Service.MouseIconEnabled = true
		if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name); ViewModel:Destroy()
			View_Model_Value = nil
		end
	end
end

local Position_ViewModel = function(ViewModel)
	if ViewModel and Humanoid.Health > 0 then
		if ViewModel:FindFirstChild("Right Arm") and ViewModel:FindFirstChild("Left Arm") then
			ViewModel["Right Arm"].CanCollide = false
			ViewModel["Left Arm"].CanCollide = false
		end

		local Rotation = Current_Camera.CFrame:ToObjectSpace(Last_Camera_CF)
		local X, Y, Z = Rotation:ToOrientation()
		Sway_CFrame = Sway_CFrame:Lerp(CFrame.Angles(math.sin(X) * Sway_Amount, math.sin(Y) * Sway_Amount, 0), 0.1)
		Last_Camera_CF = Current_Camera.CFrame

		if Humanoid.WalkSpeed > 16 then Bobble_Speed = 11.5
		else Bobble_Speed = 6.5 end

		local Bobble_Offset = CFrame.new()
		if Humanoid.MoveDirection ~= Vector3.new() then
			if not Is_Aiming then
				local Cos = math.cos(tick() * Bobble_Speed * 2) * 1
				local Sine = math.sin(tick() * Bobble_Speed) * 2
				local ZAxis = -math.sin(tick() * Bobble_Speed) * 4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis))
			else
				local Cos = math.cos(tick() * Bobble_Speed) * 0.1
				local Sine = math.sin(tick() * Bobble_Speed / 2) * 0.2
				local ZAxis = -math.sin(tick() * Bobble_Speed/ 2) * 0.4
				Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(0))
			end
		else
			local Cos = math.cos(tick() * Bobble_Speed / 5) * 0.1
			local Sine = math.sin(tick() * Bobble_Speed / 7) * 0.4
			local ZAxis = -math.sin(tick() * Bobble_Speed / 7) * 0.8
			Bobble_Offset = CFrame.Angles(math.rad(Cos), math.rad(Sine), math.rad(ZAxis)) 
		end

		if ViewModel.PrimaryPart then
			ViewModel:SetPrimaryPartCFrame(Current_Camera.CFrame * Sway_CFrame * Aim_CFrame * Bobble_Offset)
		end
		if Is_Aiming then
			if ViewModel:FindFirstChild("AimCF") then
				local Offset = ViewModel:WaitForChild("AimCF").CFrame:ToObjectSpace(ViewModel.PrimaryPart.CFrame)
				Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.2)
			end
		else
			local Offset = CFrame.new()
			Aim_CFrame = Aim_CFrame:Lerp(Offset, 0.1)
		end
	end
end

local Load_View_Model = function(Tool, NewCharacter)
	print("Child Added")
	if Tool:IsA("Tool") and NewCharacter.Humanoid.Health > 0 and Calculate_Distance(NewCharacter.Head.Position, Current_Camera.CFrame.p) < 1 then
		User_Input_Service.MouseIconEnabled = false
		if Replicated_Storage.ViewModels:FindFirstChild(Tool.Name) then
			local Choosen_Model = Replicated_Storage.ViewModels:FindFirstChild(Tool.Name):Clone()
			if NewCharacter:FindFirstChildOfClass("Shirt") then
				local Shirt = NewCharacter.Shirt:Clone(); Shirt.Parent = Choosen_Model
				print("Added Shirt")
			end
			if NewCharacter:FindFirstChild("Body Colors") then
				local Colors = NewCharacter["Body Colors"]:Clone(); Colors.Parent = Choosen_Model
				print("Added Colors")
			end
			Choosen_Model.Parent = Current_Camera
			Run_Service.RenderStepped:Connect(function()
				if Tool.Parent == NewCharacter then
					View_Model_Value = Choosen_Model
					View_Model_Value = tostring(View_Model_Value.Name)
					Position_ViewModel(Choosen_Model)		
				end
			end)
		else
			print("Didnt find tool viewmodel")
		end
	else
		print("Not a tool")
	end
end

local Tool_Invisibility = function()
	if Character:FindFirstChildWhichIsA("Tool") then
		local Tool = Character:FindFirstChildWhichIsA("Tool")
		if Current_Camera:FindFirstChild(Tool.Name) then
			local ViewModel = Current_Camera:FindFirstChild(Tool.Name)
			local Distance = Calculate_Distance(Head.Position, Current_Camera.CFrame.p)
			if Distance < 1 then
				for _, Part in ipairs(Tool:GetChildren()) do
					if Distance < 0.8 then
						if Part then
							Part.Transparency = 1
						end
					else
						if Part then
							Part.Transparency = 0
							Destroy_ViewModel(Tool)
						end
					end
				end
			end
		end
	end
end

local function characterInit()
	Character.ChildAdded:Connect(function(Child)
		Load_View_Model(Child, Character)
	end)
	Character.ChildRemoved:Connect(Destroy_ViewModel) -- Idk if this is supposed to be here?
	
	Humanoid.Died:Connect(function(died)
		if View_Model_Value then
			Destroy_ViewModel(View_Model_Value)
		end
	end)
end
characterInit()

User_Input_Service.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
		Is_Aiming = true
	end
end)

User_Input_Service.InputEnded:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
		Is_Aiming = false
	end
end)

Run_Service.RenderStepped:Connect(Tool_Invisibility)

Player.CharacterAdded:Connect(function(NewCharacter)
	Character = NewCharacter
	Humanoid = Character:WaitForChild("Humanoid")
	Head = Character:WaitForChild("Head")
	
	characterInit()
end)

(Please excuse my tardiness)

2 Likes

yea it worked, thanks for help.

1 Like

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