Gun damages player shooting it(Now it's solved!)

Guns are not supposed to damage the player using, but this is what is happening with my gun.

I dont know why it does it but uh here’s the localscript that does everything

local tool = script.Parent
local player = game.Players.LocalPlayer
local shooting = false
local mouse = player:GetMouse()
local raycastfilter = RaycastParams.new()
raycastfilter.FilterDescendantsInstances = {player.Character}
raycastfilter.FilterType = Enum.RaycastFilterType.Blacklist
local ammo = 6
local max_ammo = 6
local cooldown = 0.1
local reloading = false
local mouse_icon = "http://www.roblox.com/asset/?id=9720078100"
local UIS = game:GetService("UserInputService")
local StarterGui = game:GetService("StarterGui")
local equipped = false

local function reload()
	player.PlayerGui.RevolverGUI.TextLabel.Text = "Reloading!"
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
	tool.Handle.Reload:Play()
	reloading = true
	task.wait(2)
	ammo = max_ammo
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(max_ammo)
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
	reloading  = false
end

tool.Equipped:Connect(function()
	equipped = true
	player.PlayerGui.RevolverGUI.Enabled = true
	player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
	mouse.Icon = mouse_icon
end)

tool.Unequipped:Connect(function()
	equipped = false
	mouse.Icon = ""
	player.PlayerGui.RevolverGUI.Enabled = false
	script.Parent.Handle.Reload:Stop()
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R and equipped then
		if script:FindFirstAncestorWhichIsA("Tool").Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
			reload()
		end
	end
end)

tool.Activated:Connect(function()
	if not shooting and not reloading and script:FindFirstAncestorWhichIsA("Tool").Parent:FindFirstChildWhichIsA("Humanoid").Health > 0 then
		if ammo == 0 then
			reload()
			return
		end
		ammo -= 1
		tool.Handle.Fire:Play()
		player.PlayerGui.RevolverGUI.TextLabel.Text = tostring(ammo)
		local raycast = workspace:Raycast(tool.Handle.Position,(mouse.Hit.Position - tool.Handle.Position)*300, raycastfilter)
		if raycast then
			local instance = raycast.Instance
			local instanceParent = instance.Parent
			local findHumanoid = instanceParent:FindFirstChildOfClass("Humanoid")

			if findHumanoid then
				game.ReplicatedStorage.RemoteEvent:FireServer(findHumanoid)
			end
		end
		shooting = true
		task.wait(cooldown)
		shooting = false
	end
end)

Cast the Ray on the server and have a RemoteEvent deal with it, on the Client

Im doing raycasting on the client cause some dudes say it’s better to do it on the client

The raycast is done on the client and if it hits something it will check if it has a humanoid, if so i made the server damage the player

You do know that exploiter can just abuse that right?

Dammit i accidently clicked solution…

Try this, there’s a chance the character hasn’t loaded and is nil.

local character = player.Character or player.CharacterAdded:Wait()
raycastfilter.FilterDescendantsInstances = {character}

Ik they can

This text will be blurred

Ok i’ll try that, I should add it in the variable part?

They say raycasting should be done on the client for better performance, I’m not worried about exploiters at the moment

If you do the raycast on the client, you just could do something like:

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer


-- If raycast hits a part then
      local Player = Players:GetPlayerFromCharacter(Humanoid.Parent)
      if LocalPlayer ~= Player.Name then Humanoid:TakeDamage(5) end
-- end

Yee That fixed it, So i should always wait for the character i guess

The damaging is done on the server

Well then Fire a remote passing the humanoid and it will be done on the server.

Now i found another glitch

This text will be blurred

U mean the damaging?

This text will be blurred

This seems to work but it works once, if u die it won’t work again

This is because the character model is destroyed however the raycast filter is using this old character model.

Use player.CharacterAdded event to get the newly created character and create a new filter using this new model.

Hmm

This text will be blurred

Seems like it did 100% fix it, I’m just gonna test hold on…