Delayed Tracers

When shooting a weapon I have a system where it makes a tracer heres the code that does it:

local Part = Instance.new("Part")
Part.CollisionGroup = "BeamSpawn"
Part.Position = cast.StateInfo.Trajectories[1].Origin
Part.Parent = workspace
Part.Transparency = 1; Part.CanCollide = false; Part.Anchored = true;
local Attachment = Instance.new("Attachment")
Attachment.Parent = Part
Attachment.WorldPosition = cast.StateInfo.Trajectories[1].Origin
bullet.Beam.Attachment1 = Attachment

Now the issue is when walking around the tracers are delayed even tho the code shows almost perfect timing:
image
First - is the momement the client sent a request to shoot
Second - is the moment when the server received the info
Third - is the moment the beam was created

Now here’s the whole code:

local Physics = game:GetService("PhysicsService")

local Tool = script.Parent
local FireEvent = Tool.FireEvent
local FastCast = require(Tool.FastCastRedux)
local PartCache = require(Tool.PartCache)
local FirePoint = Tool.Weapon.ShootPart.Attachment

local BulletFolder = workspace.Bullets
local BulletStorage = game.ServerStorage.Bullets
local Bullet = BulletStorage["9mm"]
local BulletCache = PartCache.new(Bullet, 100, BulletFolder)

local Caster = FastCast.new()
local CastBehaviour = FastCast.newBehavior()
local CastParameters = RaycastParams.new()

CastParameters.FilterType = Enum.RaycastFilterType.Exclude
CastBehaviour.Acceleration = Vector3.new(0, 0, 0)
CastBehaviour.AutoIgnoreContainer = false
CastBehaviour.CosmeticBulletContainer = BulletFolder
CastBehaviour.CosmeticBulletProvider = BulletCache

CastBehaviour.RaycastParams = CastParameters

local ShootSpeed = 1000
local CanShoot = true
local NewBullet = false

local function onEquiped()
	CastParameters.FilterDescendantsInstances = {Tool.Parent}
end

local function onLengthChanged(cast, lastPoint, direction, length, velocity, bullet)
	if bullet then
		local bulletLength = bullet.Size.Z / 2
		local offset = CFrame.new(0, 0, -(length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastPoint, lastPoint + direction):ToWorldSpace(offset)	
		if NewBullet then
			NewBullet = false
			print("Third: ", os.clock())
			print("Position: ", cast.StateInfo.Trajectories[1].Origin)
			print("-------------------------")
			local Part = Instance.new("Part")
			Part.CollisionGroup = "BeamSpawn"
			Part.Position = cast.StateInfo.Trajectories[1].Origin
			Part.Parent = workspace
			Part.Transparency = 1; Part.CanCollide = false; Part.Anchored = true;
			local Attachment = Instance.new("Attachment")
			Attachment.Parent = Part
			Attachment.WorldPosition = cast.StateInfo.Trajectories[1].Origin
			bullet.Beam.Attachment1 = Attachment
			delay(.2, function()
				Part:Destroy()
			end)
		end
	end
end

local function onRayHit(cast, result, velocity, bullet)
	local hit = result.Instance
	bullet.HIT:Emit(3)
	
	local character = hit:FindFirstAncestorWhichIsA("Model")
	if character and character:FindFirstChild("Humanoid") then
		if hit.Name == "Head" then
			character.Humanoid:TakeDamage(20)
		else
			character.Humanoid:TakeDamage(10)
		end
	end
	
	delay(20, function()
		BulletCache:ReturnPart(bullet)
	end)
end

local function fire(plr, mousePos)
	if CanShoot then
		print("Second: ", os.clock())
		print("Position: ", Tool.Weapon.ShootPart.Attachment.WorldPosition)
		CanShoot = false
		NewBullet = true
		Tool.Weapon.ShootPart.Attachment.SHOOTFX:Emit(3)
		Tool.Handle.ShootSound.EmitterSize = 5
		Tool.Handle.ShootSound:Play()
		local Orgin = FirePoint.WorldPosition
		local Direction = (mousePos - Orgin).Unit
		Caster:Fire(Orgin, Direction, ShootSpeed, CastBehaviour)
		wait(.1)
		CanShoot = true
	end
	
	
end

FireEvent.OnServerEvent:Connect(fire)
Tool.Equipped:Connect(onEquiped)
Caster.LengthChanged:Connect(onLengthChanged)
Caster.RayHit:Connect(onRayHit)

And here’s a video demonstrating the issue:

For reference I’m using FastCast

Hi . Let me explain why this happens . When your player tells the server that the weapon os fired, it takes around 80ms(depending on ping). Lets say now you make a projectile on the server to visualise the shot . That will cause more delay as the server have to tell the clients that a new part/projectile is created.

Solution

  1. render the shot visual before it is even sent to the server
    2)render the projectile client side(where the server tells all the clients about the origin , direction, what not to each client. The client will then render the shot
    3)make the entire gun system client sided(this is very very very hard)

The issue with FastCast to my knowledge is that I do not have the bullet object on the client so I have to call the server then in the server make the shot and then get the bullet

ok so , when lets say your clients fire the gun . You will send the server the fire data like origin direction ect . Now , instead of the server creating the projectile, get the server to send ALL the clients the firedata . When each of the client receive the firedata , render the projectile on the client