Is this a good way of hit detection in a game?

So here is the thing: I’m making an fps shooter, and I use this system to make the shooting way smoother, and I want to know if this is good or bad.

example:

When a player shoots a gun,the client will shoot a raycast, and if the raycast hits another player, the client fires to the server, sending the location where the raycast hit, and the server deals the damage.

I want to know if this is good or bad for an FPS. If you know a better way to do this, please tell me.

5 Likes

Best practice is to do as much as you can on the server so you can eliminate as many possibilities for cheating as you can. Firing the ray from the client allows cheaters to manipulate it however they want, and it makes sending it to the server for validation useless if they already messed with it. Collect all the data you need from the client, and then send it to the server to fire the ray, and process anything else you need to do after a hit.

2 Likes

Thanks for responding, but the problem is that it has a little delay when done that way.

What you can do is to spawn a fake bullet on every client. Then, in the server, spawn an invisible bullet which is the thing that deals damage.

1 Like

I have done similar. Other methods such as only casting the ray on the server are also common, but from what I have tried myself (although I may not implement it properly) this can hurt players with higher ping and ruin the experience for them. You method will be fine if you have the proper sanity checks, for example:

  • check if the ray hits any walls before hitting the target
  • check if the ray is pointed in a similar direction to where the player is facing
  • if your guns have a max range, check the distance between the 2 players
  • check if the shooter is even able to see the person who was hit. dont just check head to head. check from the shooters head to a few anchor points on the recipient’s body such as center of mass, head, and the ends of limbs.
2 Likes

Ok nice , so i will do this: i will cast a raycast on the client and on the server if the client hit s a player but the server does not hit i will cancel the damage so it isn’t unfair for the player whit high ping. Please tell me if this is good

1 Like

give this a read Security Tactics and Cheat Mitigation | Documentation - Roblox Creator Hub. Scroll down to Weapon Targeting.

2 Likes

What I would do is have the client call the server event either way, it can call with nil or with a player’s limb that it hit. This way the server can track the ammo a player has left. The client sided raycast can be used to display things that the server doesn’t care about. This can be hitmarkers, blood particles, whatever graphical effects that don’t affect other players.

heres a little example:

local function showHitMarker(headShot)
	if headShot then
		print("You hit a headshot, nice!")

	else
		print("You hit a body/limb shot")
	end
end
local raycastParams = RaycastParams.new()
--customize however you want here
local function createRayCast()
	local lookDirection = workspace.CurrentCamera.CFrame.LookVector
	local origin = workspace.CurrentCamera.Position
	local raycastResult = workspace:RayCast(origin, lookDirection, raycastParams)
	--this will let the server know who/what the raycast hit (if it hit anything) and
	--a lot more additional information that can be used
	remoteEvent:FireServer(raycastResult)
	--all of this will be client sided. the server doesn't care what happens here as it is
	--all for visual effects
	if not raycastResult then return end
	if not raycastResult.Instance then return end
	local hit = raycastResult.Instance
	if hit:FindFirstAncestorOfClass("Model") and hit.Parent:FindFirstChildOfClass("Humanoid") then
		local headShot = false
		if hit.Name == "Head" then
			headShot = true
		end
		showHitMarker(headShot)
	end
end

sorry if there are bugs, i wrote this straight into the reply box and not in an actual code editor

2 Likes

Thanks everyone for replying. Now i know what to do with my code so my game isn’t bad .thank you so much everyone

2 Likes

Also, go ahead and read what @RobloxRulesAbider sent if you want to become even more proficient in programming. I learned a bit from it, and it wont hurt you at all. It takes at most 20 minutes and it is put in the simplest terms possible.

2 Likes

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