Tool/Gun doesn't function properly after player died

My AK-47 gun will usually work on start but after the player’s character dies, it will no longer function properly. I asked ChatGTP what the problem could be and I tried the response but it comes to no use, so I’d really appreciate if someone could help me.

local playerService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local replicateRemote = replicatedStorage.Replicate
local hitRemote = replicatedStorage.Hit
local player = playerService.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local playergui = player:WaitForChild("PlayerGui")
local gunui = playergui:WaitForChild("GunUI")
local gunframe = gunui:WaitForChild("GunFrame")
local ammotext = gunframe.AmmoText
local guntext = gunframe.GunText
local mouse = player:GetMouse()

local tool = script.Parent
local firePoint = tool.Main 
local gunSettings = require(tool:WaitForChild("Settings"))
local equipped = false
local ammo = tool:WaitForChild("AmmoValue")
local doFire = false

local function updateText()
	
	guntext.Text = script.Parent.Name 
	ammotext.Text = ammo.Value.."/"..gunSettings.maxamo
end

ammo:GetPropertyChangedSignal("Value"):Connect(function()
	updateText()
end)


local function onEquipped()
	if equipped and gunui.Enabled then
		equipped = false
		gunui.Enabled = false
	elseif not equipped and not gunui.Enabled then
		equipped = true
		gunui.Enabled = true
	end
	guntext.Text = script.Parent.Name 
	ammotext.Text = ammo.Value.."/"..gunSettings.maxamo
	print("Equipped "..tostring(equipped))
end

tool.Equipped:Connect(onEquipped)

tool.Unequipped:Connect(onEquipped)



local debris = game:GetService("Debris")

local function castRay()
		local ignoreList = {character, workspace.Effects, workspace.CurrentCamera}
	local origin = firePoint.Position
	local direction = (mouse.Hit.p - firePoint.Position).Unit
	direction = direction * gunSettings.range

	local ray = Ray.new(origin, direction)
	local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

	replicateRemote:FireServer(tool, origin, pos)
	local visual = Instance.new("Part")
	local length = (pos - origin).Magnitude
	visual.Anchored = true
	visual.CanCollide = false
	visual.Material = Enum.Material.Neon
	visual.Color = gunSettings.rayColor
	visual.Size = Vector3.new(gunSettings.raySize.X, gunSettings.raySize.Y, length)
	visual.CFrame = CFrame.new(origin, pos) * CFrame.new(0,0,-length/2)
	visual.Parent = workspace.Effects
	debris:AddItem(visual, gunSettings.debrisTime)

	return hit, pos, direction, origin
end


local function gunEffects()
	for _, effect in pairs(firePoint:GetChildren()) do
		if effect:IsA("ParticleEmitter") then
			effect:Emit(50)
		end
		
		if effect:IsA("Sound") then
			effect:Play()
		end
	end
end



local function fire()
	local waitTime = 60/gunSettings.rateOfFire

	repeat
		if equipped and not tool.Debounce.Value and ammo.Value > 0 then
			tool.Debounce.Value = true

			delay(waitTime, function()
				tool.Debounce.Value = false
			end)

			gunEffects()
			local hit, pos, direction, origin = castRay()
			ammo.Value -= 1
			updateText()

			if hit then
				local relCFrame = hit.CFrame:Inverse() * CFrame.new(pos)
				hitRemote:FireServer(tool, hit, direction, origin, relCFrame)
			end
		end
		wait(waitTime)

	until not equipped or not doFire or gunSettings.fireMode ~= "AUTO"
end



local function onCharacterAdded(character)
	wait(1)
	ammo.Value = 30
	updateText()
	mouse = player:GetMouse()
	mouse.Button1Down:Connect(function()
		doFire = true
		fire()
	end)
	mouse.Button1Up:Connect(function()
		doFire = false
	end)

	while character.Parent ~= workspace do
		character = player.Character or player.CharacterAdded:Wait()
		wait()
	end
	updateText()
end

if player.Character then
	onCharacterAdded(player.Character)
end

local function onCharacterRemoving()
	doFire = false

	equipped = false
	gunui.Enabled = false
	guntext.Text = ""
	ammotext.Text = ""

	mouse.Button1Down:Disconnect()
	mouse.Button1Up:Disconnect()
end

player.CharacterRemoving:Connect(onCharacterRemoving)

player.CharacterAdded:Connect(onCharacterAdded)

The problem is it won’t fire castRay function even though the gun is being fired and and I think that’s the cause of the main problem.

Also the problem I can guarantee is not due to any object not being there, I have the correct remote events in replicated storage.

In this video the first time the gun works perfectly fine but not after I die.

All replies appreciated, thank you.

1 Like

Is the script located in the tool (gun)?
If not where is the script?
Is it a local or server script?

1 Like

The script is a local script located in a tool/the gun yes.

1 Like