I’m making a gun system, and even when I visualize the bullet on all clients, I’ve noticed there is some visible delay between when the player clicks and when the bullet gets visualized. This is even worse at higher ping, but for some reason I’ve seen some games that do not have this issue even at high ping?
Server Script
local function fireBullet(mousePosition: Vector3)
local origin = muzzleAttachment.WorldPosition
local direction = (mousePosition - origin).Unit * Constants.BULLET_LENGTH +
Vector3.new(random:NextInteger(-Constants.RAY_SPREAD, Constants.RAY_SPREAD),
random:NextInteger(-Constants.RAY_SPREAD, Constants.RAY_SPREAD),
random:NextInteger(-Constants.RAY_SPREAD, Constants.RAY_SPREAD))
local raycastResult = Workspace:Raycast(origin, direction, raycastParams)
local intersection = raycastResult and raycastResult.Position or origin + direction
local distance = (origin - intersection).Magnitude
replicateBulletRemote:FireAllClients(gun, intersection, distance) -- Visualize the bullet for all client
if raycastResult then
local part = raycastResult.Instance
local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
if not humanoid then
return
end
if humanoid.Parent:HasTag("Enemy") then
humanoid:TakeDamage(Constants.BULLET_DAMAGE)
end
end
end
Bullet-Replicator Script
local function onReplicateBulletEvent(gun: Tool, intersection: Vector3, bulletLength: number, raycastPosition: Vector3?, raycastNormal: number?)
if gun and gun:IsDescendantOf(game) then
local handle = gun.Handle
local muzzleAttachment = handle:FindFirstChild("Muzzle", true)
if muzzleAttachment then
muzzleAttachment.Smoke:Emit(5)
muzzleAttachment.Flash:Emit(5)
end
local newBulletVisual = bulletVisual:Clone()
newBulletVisual.Size = Vector3.new(0.1, 0.1, bulletLength)
newBulletVisual.CFrame = CFrame.lookAt(muzzleAttachment.WorldPosition, intersection) * CFrame.new(0, 0, -bulletLength/2)
newBulletVisual.Parent = bulletVisualsContainer
local bulletFadeTween = TweenService:Create(
newBulletVisual,
TweenInfo.new(.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
{Transparency = 1})
local bulletSizeTween = TweenService:Create(
newBulletVisual,
TweenInfo.new(.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out),
{Size = Vector3.new(0.025, 0.025, bulletLength)})
local bulletColorTween = TweenService:Create(
newBulletVisual,
TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out),
{Color = Color3.fromRGB(100, 100, 100)})
bulletFadeTween:Play()
bulletSizeTween:Play()
bulletColorTween:Play()
task.delay(.5, function()
newBulletVisual:Destroy()
end)
end
end
replicateBulletRemote.OnClientEvent:Connect(onReplicateBulletEvent)