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.
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.
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.
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
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
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.