Hitboxes not responsive in roblox FPS

So im trying to add hitboxes to my game but they are not that great. i have parts welded to the player so they follow the player and this is my raycasting code to detect what the player shoots at in my fps ` local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Players = game:GetService(“Players”)
local Teams = game:GetService(“Teams”)
local FireEvent = ReplicatedStorage:WaitForChild(“FireEvent”)
local CanShoot = ReplicatedStorage:WaitForChild(“CanShoot”)

FireEvent.OnServerEvent:Connect(function (player,BarrelPos,MousePos,Damage,HeadShotDamage)
local ray = Ray.new(BarrelPos,(MousePos - BarrelPos).unit * 99999999)
local part = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
if part then
  local humanoid = part.Parent:FindFirstChild("Humanoid")
  local Enemy = game.Players:GetPlayerFromCharacter(part.Parent)
  if humanoid and humanoid.Parent.Name:lower() == " " then
		if humanoid and not humanoid:IsDescendantOf(player.Character) then
			if part.Name:lower() == "head" then humanoid:TakeDamage(HeadShotDamage) else humanoid:TakeDamage(Damage) end
		end
  end
  if Enemy then
	    local tag = Instance.new("ObjectValue")
        tag.Value = player -- this can either be the shooters character or plr object (adjust accordingly if you use character)
        tag.Name = "creator"
        tag.Parent = Enemy.Character.Humanoid -- this is the enemies humanoid, again can be parented to their character (adjust to it if you use character)
        game:GetService("Debris"):AddItem(tag,.5) -- destroys tag after half a second
  end
 if CanShoot.Value == true then
  if humanoid and not humanoid:IsDescendantOf(player.Character) then
			if part.Name:lower() == "headhitbox" or part.Name:lower() == "head" then
				humanoid:TakeDamage(HeadShotDamage) 
				print("head shot")
			elseif part.Name:lower() == "bodyhitbox" or not part.Name:lower() == "head" then
				humanoid:TakeDamage(Damage) 
				print("body shot")
			end
end
end 
end

end)` im not sure what im missing and how to make it respond better. i know that raycast hitbox thing exists but i have no idea how to get it working for something like this. because the github doesnt have anything like this. any help would be great. i dont know how other games tackle with hitboxes and im not sure how they make it responsive

Can you clarify what you mean by responsive?

i mean most of the time the hits dont register and the player does not get damaged at all
sorry for the late response

I don’t see anything immediately obvious but I do have two critiques.

  1. You are using deprecated Raycasting. Use WorldRoot:RayCast instead.
  2. 99999999 is a very excessive value for the magnitude of the ray. This could be causing your problem but I am not sure.

how do i make the ray go on forever though? because my game’s maps are kind of big and i want the rays to be able to go very far

I’m not saying you can’t have a super long rays, 99,999,999 is just way too much. I can’t imagine you needing to cast a ray over a thousand studs. If your maps are really big then you should probably make a projectile that moves each frame so that the math isn’t all done on a single frame.

You’re doing the hit detection on the server which means that it will be delayed, depending on your ping. You’ll have to do it on the client if you want it to be more responsive.

but if its happening on the client, wouldnt the other players not recive the damage?

Use Remoteevents to tell the server that something was hit.

you need to raycast on the client, then pass the part/player it hit to the server

ok im going to try it now i’ll tell you if it works

yeah i cant get it working. it wont work but switching to the non depriciated raycasting is helping i think