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:
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