How to make aim system with raycast

Hello i recently started making a fps game and added aim system however it detects the player head instead of the enemy’s head

here is the video:

here is the script:

SS.OnServerEvent:Connect(function(player, mouse, attc_pos, tpp_flash)
	local DIR = (mouse - attc_pos).Unit
	local DIS = (mouse - attc_pos).Magnitude
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.FilterDescendantsInstances = blacklist
	
	local result = workspace:Raycast(attc_pos, DIR * 300, raycastParams)
	
	tpp_flash.Enabled = true
	tpp_flash.flash_img.Rotation = math.random(-90, 90)
	if result then
		local hitpart = result.Instance
		local char = hitpart.Parent
		local human = char:FindFirstChild("Humanoid")
		local DH = require(script.DH)
		local bodyparts = hitpart.Name
		if char.Name ~= player.Name and human then
			human:TakeDamage(DH.damage(bodyparts))
			if human.Health <= 0 then
				if human.Health ~= -DH.damage(bodyparts) then
					if players:FindFirstChild(char.Name) then
						players[player.Name].leaderstats.Kills.Value += 1
						players[char.Name].leaderstats.Deaths.Value += 1
					end
				end
			end
		end
	end
	task.wait(.1)
	tpp_flash.Enabled = false
end)
2 Likes

Did you add the player’s character into the raycastParams.FilterDescendantsInstances?

1 Like

its server side so if i add the player model then other players cant hit that player

1 Like

I don’t know what’s on the blacklist, but you can do it

raycastParams.FilterDescendantsInstances = {table.unpack(blacklist), player.Character}
1 Like

i dont think u understanding here if i added a player model to the blacklist the raycast results with turn nil

1 Like

It only stops the player firing into the FilterDescendantsInstances. So all other players are not in it and can still hit you. It merges your blacklist with the character of the player who is shooting, so it only ignores everything that is in that player’s character, not all players. But give it a try

1 Like

If you form a new tuple by putting an existing tuple in the beginning and other values after the existing tuple, only the first value in the existing tuple will be in the new tuple.

With your code, RaycastParams.FilterDescendants instances would only contain the first Instance in blacklist, and player.Character.

Thus, if you want to use a tuple in forming a new tuple, put it at the end.

raycastParams.FilterDescendantsInstances = {player.Character, table.unpack(blacklist)}
4 Likes

The raycasting should be done on the client so that you can add the player to the blacklist and the damage code should be on the server

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