Projectile Replication

I’m trying the FireAllClients method for projectile replication, I need help with syncing the client projectiles with the server projectile.
I have a very basic idea (shown below) to somehow gauge the distance lapsed by using the tick function on the client. Any ideas would be helpful. Thanks!

FireServer;
start = tick()

OnClientEvent;
print("Time passed: ",tick() - start)

2 Likes

If the projectile is on the server do you really need to replicate it on all of the clients individually?

The way I would go around it, for showing projectile replication, would be the following.
(Note that projectiles and other visual elements should always be done on the client, not on the server.
The server is just there to receive, validate and respond to events.)

When a client fires something:

  1. Start the projectile on the client
  2. Tell the server
  3. Ignore the reply from the server about the projectile, since the client already saw a projectile.
  4. The other clients will determine if they should render the projectile, depending on distance (and/or if it is in their viewport)

I will be providing a short pseudocode below, so you can see how it works.

Code
.onGunFired ( function ()
    startProjectile()
    remote:FireServer(target, etc..)
end)

remote.onEvent( function (event, ...)
    if event == "projectile" then
        local userShot, targetShot = arg[1], arg[2]
        if userShot ~= localUsername then
            -- Do the projectile
            startProjectile()
        end
    end
end)

For smoothness, yeah. I tried making a server only projectile with CFrame and Raycasting, needless to say it still jittered. I want hit detection to be on server and the rendering on the client if that’s even possible. Thanks for your reply

1 Like

Thanks for the reply, I don’t see how this will sync the projectile for all clients though. If there’s any way to get a near-perfect sync for all clients I’ll go through the work to do it.

Well, there is, of course, but then you would have to sacrifice the visual experience for the client that shoots.

It will be like shoot → wait .5 seconds → “oh, now the bullet is off”.
It will be a horrible experience.

But to the real question, why would you need something like this?

I don’t think replicating the projectile on the client would fix your issue with jittering, perhaps look towards other methods of moving the object.

Perhaps try using TweenService?

I just want to clarify, you don’t use the projectiles for actual hit detection, do you?

So I should render the projectile on the client that clicked first, then when the server reads it, fire the other nearby clients as well as the server itself?.. That’s a working system but I don’t see how I can do hit detection on the server that way if the origin client’s projectile is ahead of the server one… Another option would be to have the hit detection on the client but that opens the door for exploitation.

I’m a bit new to the replication thing, but I used rays for hit detection on the server.

Alright, I just have a question for you.

When do you use rays to figure out if the player would hit the other player? After they touched the projectile, or immediately after receiving the event that the player shot?

To make it easier:
A) Client shoots → Server receives event → Server checks if the player would hit
B) Client shoots → Server creates projectile → Server checks if the player would hit, when the projectile hit the other player

I’m not sure which one of the two to use with this method, i’m guessing i’d use B

Mhm, that’s where the problem lies. You should not use method B :smile:

Let me explain how you should do this.

On the server side:


When the server receives the event (when a client fired), it should immediately check if the player hits the other player (victim). Then, you would use the Rays method you mentioned, or other ways, to check if the player actually hits the victim. Either way, once you have done that, it’s time to notify the other clients that this player fired the gun. - Make sure to include the player who shot, and the player that got hit (if any), amount of damage etc.

You should also deduct the damage of the shot from the victim’s health, and all the other stuff that comes with shooter games. (I’m pretty sure you knew this already :stuck_out_tongue:)

The client who fired


Once this client fires the gun, a projectile would come out immediately, to give the player an enhanced impression of the game. They don’t want to see their bullets shoot .5 seconds after shooting!!
Then, use client-sided hit detection to see if they hit their victim, if they did, you can play some sound or show that you damaged them.

This client should ignore the incoming event from the server about the projectile, but it could get to know the damage made (client and server don’t always agree, since there is a delay).

On the other clients


When the remaining clients receive the projectile event, all they have to do is to figure out if they want to render the projectile. Use distance, if they are in the viewport, and their quality settings to determine whether to render the projectile or not.

TL;DR

Client shoots → Client renders projectile → Tells server → Server notifies other clients → Other clients render (or not) the projectiles.

25 Likes

You cleared that up nicely, thank you very much! :grinning:

1 Like