Projectile spawn problem

The projectiles don’t spawn where there supposed to spawn while the character is moving/running, the projectile will spawn a bit behind the bullet spawn point, Its hard to explain the problem, so here’s a link to the game:
https://www.roblox.com/games/6927193723/Gun-system

Please play the game before giving a solution to the problem.

How can I do this?

1 Like

Looks to me that you are creating the bullets on the server. The reason for the bullet appearing to lag behind is due to replication delay because of information from the client being sent to the server and vice versa.

There’s nothing much you can do about it really, you could do some lag compensation (this is a headache and still won’t fix it fully).

If the game is going to be competitive then I would recommend firing bullets on the client instead (Yes this will make it vulnerable to exploits, but having the replication delay will ruin the experience on competitive games).

2 Likes

If I replicated the bullets on the client side, then how would other players see the bullets?

When you fire a bullet on the client, you will have to use a remote event that sends the details to the server, which then sends that to each client so they can replicate it too.

1 Like

Would I have to make a remote event to tell a server script to tell each client to fire a bullet from that one specific player that fired the bullet?

How would I do that? Like by enabling and disabling a client script?

I’m not sure where to start :sweat_smile:, could you provide some example code of how I can tell all clients to replicate the bullet?

When you fire a bullet on the client, you also fire a remote event to the server, containing the target position or direction.

Once the server receives this you then want to send that info to all clients. You don’t want the client who sent the remote to receive it though.

You could do something along the lines of:

OnServerEvent:Connect(function(playerWhoFired, targetPosition)
    for _, player in ipairs(players) do
        if player ~= playerWhoFired then
            remote:FireClient(player, targetPosition)
        end
    end
end)

How would I tell the other clients the position of the replicated bullet?

Each client will know the player who fired and the target position. So then each client can then create a new projectile starting at the player’s character, going in the direction of target position.

So, I should change "player", with the bullet spawn point? (for example in a gun)

When the client recieves the player who fired along with the target position, you could treat the player.Character.PrimaryPart.Position as the spawn point for example

In this case, is the “player” the starting position for the projectile? or are we simply defining the player?

The first argument for :FireClient is the target player/client to fire the remote to.

Just making a small change to this,

OnServerEvent:Connect(function(playerWhoFired, targetPosition)
    for _, player in ipairs(players) do
        if player ~= playerWhoFired then
            remote:FireClient(player, playerWhoFired, targetPosition)
        end
    end
end)

Then on the client,

OnClientEvent:Connect(function(playerWhoFired, targetPosition)
    --create a bullet at playerWhoFired.Character and make it move torwards targetPosition
end)

But targetPosition is the mouse, right? So then how would I define the gun that the player who fired the bullet is holding, so I can set the bullet Starting Position?

EDIT:

Ah, I see. So, I would have to do something like this?
PlayerWhoFired.Backpack.Gun.BulletSpawnPoint, to set the starting position for the bullet.

There’s a lot of flexibility in how you do this. For the sake of simplicity I would just have the bullet spawn in the middle of the character, or at their head like a lot of FPS games do, if you wanted you could also find the gun model inside the character and have it spawn there.

I was assuming targetPosition would be the Mouse.Hit.Position. If you are doing something else you can just make it anything that will indicate a direction or target position for the bullet to travel to.

When the gun is equipped it will be in the character model, it’s only in the backpack when unequipped. If you are having it spawn at the gun then you will also need to do checks in case the gun was unequipped before the remote event reaches the client.

Alright, I’m going to try and do this and I’ll let you know how it goes. :smiley: :+1:

What’s the difference between "player" and "playerWhoFired" ?

Is "player" referring to the client we just sent the server info to, and "playerWhoFired" referring to the player who fired the bullet?

Shoudnt there also be a "player" on the OnClientEvent?

Thanks for the help! :smile: :+1: It now works fine, It fixed the problem and it doesnt have any replication delay, and it also fixed the Network Ownership problem that I had, I marked your first reply as the solution, Thanks :smiley: