Server only sometimes takes damage

So for my gun only sometimes the humanoids take damage.
I added print statements so you know what happens.

game.ReplicatedStorage.Events.Fire.OnServerEvent:Connect(function(player, pos, damage, target)
	print("Server Received")
	local gun = player.Character:FindFirstChild("Pistol")
	
	if gun == nil then return end
	
	local muzzle = gun:FindFirstChild("Muzzle")
	local direction = (pos - muzzle.Position)
	
	local rayparams = RaycastParams.new()
	local character = player.Character:GetChildren()
	rayparams:AddToFilter(character)
	rayparams:AddToFilter(gun:GetDescendants())
	rayparams.FilterType = Enum.RaycastFilterType.Blacklist
	
	muzzle.Laser:Play()
	
	local ray = workspace:Raycast(muzzle.Position, direction, rayparams)
	print("Ray on target")
	if ray == nil then return end 
	print("Humanoid is real")
	local Humanoid = ray.Instance.Parent:FindFirstChild("Humanoid")
	
	if Humanoid == nil then return end
	Humanoid.Health -= 20
	print("damage taken")
end)

Here’s a video.

And a image.

Can someone tell me what’s wrong with the script

Could be that you need to extend the ray past where the player clicks. If the ray is stopping just a hair too soon because it ends exactly where the player clicks then it wont register a target.

local direction = (pos - muzzle.Position) * 2

Thank you for fixing my issue as I just realized that in the command bar anytime the ray didn’t register damage, it never print the phrase that happens when a ray hits it’s target.

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