How to make a client side hit detection gun?

So , I am trying to make a gun system that have a client-side hit detection to prevent lag . How do I safely ensure that the hit is valid? The gun use a CFrame projectile and not just a raycast

1 Like

The only thing i can think of is mouse.hit
Mouse | Documentation - Roblox Creator Hub.
Sadly it has been superseded

1 Like

One thing I can think of is that players can use hitbox extenders, meaning the size of the player’s part is enlarged to get hit easily. You can compare the size on the client side and pass that information to the server to see if the size remains the same.

1 Like

if you know how to raycast you should maybe try to make a raycast from the projectile’s position hitpart position and check if the distance is too big to be legtimate,just dont do it in the local script.

but doing things in sever script make it very laggy

still,its the best option if you are using moving projectiles instead of raycasts atleast from what i know

Something like this should work …

local gun = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

gun.Activated:Connect(function()
    local origin = gun.Handle.Position
    local direction = (mouse.Hit.p - origin).unit
    local hit = workspace:Raycast(origin, direction * 100)

    if hit then
        print("Hit " .. hit.Instance.Name .. " at position " .. hit.Position)
    end
end)

but the server will not detect the hit unless i fire a remote event .

 if hit then
      fire the remote
 end

You asked for a client side hit detection … now you talking about a server script … ?

This is probably very bad practice, It will be very very easy for exploiters to take advantage of a local script, and If you want any of the “client side” shots to be visible to the server, you will need remote events either way. Unless you don’t need to run the script on the server for some strange reason, this doesn’t make sense to me.
Note: the best way in my opinion is to use Mouse.Hit to get the location of where you’re shooting and raycast from the origin.

How I do it:

Raycast from start (origin) to end (destination). You should have two respective cframes. Send that to the server. When you fire the event to invoke the action on the game, fire on the client in parallel so that it only renders if and and only if the player is in range of the bullet. Best way to do this is a spherecast. The size of the sphere should be based on the maximum distance from start to end. Whatever it hit, render for those players only.

All guns are client side hit detection btw. Running it purely on client would be exploitable. Running it on purely the server wouldn’t be innately feasible. If you send where your projectile started and ended to the server to keep track of, you eliminate a lot of exploits that may try to alter that on the client. If you render what the client can only see, you make the overall system more performant per client.

1 Like

Thank you! This explanation is very helpful!

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