Client Replication Help

  1. What do you want to achieve?
    I’m trying to make client to server to client replication for projectiles in my game, I’ve made the scripts simpler than what I have to show the basic idea and problem

  2. What is the issue?
    How do I replicate the part if a player didn’t hit anything, and the part only replicates when it hits something so there’s a lot of latency from when the player actually fires the part. I also had issues with parts replicating multiple times before, if I move the remote fireserver

the latency
https://gyazo.com/f99760a27549cdc8766e836dd961dd3d

when you hit nothing
https://gyazo.com/71bb6e5b8ef08b44bf42899db2b84949

the module script

local module = {}


function module.Projectile(character)
	
	local remote = game.ReplicatedStorage.Events.RemoteEvent
	local torso = character.HumanoidRootPart
	local projectile = game.ReplicatedStorage.Part:Clone()
	local hit = false
	
	local linearvelocity = Instance.new("LinearVelocity")
	
	local linearattachment = Instance.new("Attachment")
	
	linearvelocity.MaxForce = 25000
	linearvelocity.Attachment0 = linearattachment
	linearvelocity.VectorVelocity = torso.CFrame.LookVector * 50
	linearvelocity.Parent = linearattachment
	linearattachment.Parent = projectile
	
	projectile.Position = torso.Position + torso.CFrame.LookVector * 5
	projectile.Parent = workspace
	
	local overlap = OverlapParams.new()
	overlap.FilterDescendantsInstances = {character, projectile}
	overlap.FilterType = Enum.RaycastFilterType.Exclude
	
	local heart = game:GetService("RunService").Heartbeat:Connect(function()
		
		local hitbox = workspace:GetPartsInPart(projectile)
		
		for i,v in pairs(hitbox) do
			if hit then return end
			hit = true
			
			remote:FireServer(v.Position, projectile.Name, torso.Position)
		end
	end)
	
	task.wait(1)
	heart:Disconnect()
end

return module

the local script

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local uips = game:GetService("UserInputService")
local module = require(game.ReplicatedStorage.ModuleScript)

local remote = game.ReplicatedStorage.Events.RemoteEvent

local tween = game:GetService("TweenService")

uips.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		module.Projectile(character)
	end
end)

remote.OnClientEvent:Connect(function(hitposition, attackname, origin)
	
	local attack = game.ReplicatedStorage[attackname]:Clone()
	
	attack.Position = origin
	attack.Anchored = true
	attack.Parent = workspace
	
	tween:Create(attack, TweenInfo.new(), {Position = hitposition}):Play()
end)

the server script

local remote = game.ReplicatedStorage.Events.RemoteEvent
local players = game:GetService("Players")

remote.OnServerEvent:Connect(function(player, hitposition, attackname, origin)
	
	
	for i,v in pairs(players:GetPlayers()) do
		if v ~= player then
			-- different player
			
			remote:FireClient(v, hitposition, attackname, origin)
		end
	end
	
	
	
end)
1 Like