Problems with Lagging Bullets

Goal:

     I’m trying to create a SciFi blaster system in Roblox using Lua. The way I decided to go about this is using a raycast to apply the damage and a glowing bullet projectile with no collision to show the path of the bullet. This seemed like a good idea because the projectiles are lasers so they don’t need to have gravity.

Problem:

     The problem is that the bullets lag and then teleport through the air to catch up. This only happens in the published game, not in studio, which means (I think) that the problem has to do with the connection between the Client and Server, because Studio uses a Local Server setup. You can see the lagging here.

Attempted Solutions:

     I thought I’d messed something up with one of the scripts I created, so I tried using the FastCast system to get an idea of its performance. However, the FastCast bullets lagged as well, leading me to believe that the problem may be somewhere else. I tried the weapons in an empty map, but the problems persisted. I also tried connecting the bullets movement to both the Heartbeat and the Stepped options.

Scripts:

Here is the script inside the bullet:

local RunService = game:GetService("RunService")

local blast = script.Parent
local studsPerSecond = blast:GetAttribute("Speed")
local timeElapsed = 0

local function MoveBullet(deltaTime)
	local speed = -1 * studsPerSecond * deltaTime
	blast.CFrame = blast.CFrame * CFrame.new(0,0,speed)
	
	timeElapsed += deltaTime
	if timeElapsed > blast:GetAttribute("Lifetime") then
		blast:Destroy()
	end
end

RunService.Stepped:Connect(function(totalTime, deltaTime)
	MoveBullet(deltaTime)
end)

Thank you for any help you can offer and please let me know if I need to add anything to the post for greater clarity.

Hi!

Have you tried using Network Ownership?

https://create.roblox.com/docs/scripting/networking/network-ownership

Saludos!

This is definitely a issue with creating the visuals server-side. I recommend you to instead handle hit-detections on the server, and the visuals on clients side exclusively. FastCast has not been the issue, it’s just that you need to handle it on the client.

Thanks for the suggestion. I tried setting the Network Ownership to nil but the bullet still lagged.

How would you suggest I do this for all of the clients? Do I need to create a projectile for each client every time any of them fire?

It is pretty easy, it should work like this:
A player fires ;

The client fires a remote to the server to handle the hitbox, and the player’s client handles the visuals for that client ;

Once the server gets the remote, you do a for loop, loop through all players, check if the player you are looping through is NOT the player who sent the remote (the player who sent the remote is the first argument being sent through) ;

Then, fire a remote from the server to that player’s client.

Back on the client, each client will handle the visuals once it receives that remote. The remote from the server to the client should provided all the info like projectile type and where its trajectory, etc. Make sure it only is handling visuals, all hitboxes and damage dealing should be handled on the server.

3 Likes

I do not ask you to copy his code, but I saw a reply under the FastCast module post on DevForum which pretty much explains what you need to do. I will link you Quoteory’s post here.

Thank you! Your solution was clear, it worked, and I was able to implement it quickly.

1 Like