Projectile lags when spawned on server (Help Wanted!)

I know that spawning projectiles on server is a pretty bad practice so I strongly need and advice on how to move it to module or client script! I understand more or less which part I should put where, but how no idea how to connect them! How should I separate rendering projectile and hit detection and *GET RID OF THIS LAG??? Been trying to figure it out for a couple of weeks so help will be MUCH appreciated. So far projectile stutters sometimes or behaves as tho having some crazy velocity.

The video of the lag

Server Script
---Server script 
local ts = game:GetService('TweenService')
local Debris = game:GetService("Debris")
local rep = game:GetService('ReplicatedStorage')
local rs = game:GetService("RunService").Heartbeat;

t = 0
script.Parent.WeaponFired.OnServerEvent:Connect(function(player, mousepos)
	local star = rep.StarHitbox
	local ch = player.Character
	local hum = ch.Humanoid
	local hrp = ch:WaitForChild('HumanoidRootPart')
	local righthand = ch:WaitForChild('RightHand')
	local tool = ch:FindFirstChild('Star')

	local starclone = star:Clone()
	starclone.CanCollide = true
	starclone.Parent = workspace
	starclone:SetNetworkOwner(player)
	starclone.Orientation = Vector3.new(0, math.rad(-90), 0)




	local distance = (mousepos - hrp.Position).magnitude
	local x = print(distance)

	local function set_t(distance)
		if distance <= 300 then
			t = 0.3
		elseif distance > 300 and distance < 1000 then
			t = 0.5
		end
		return t
	end

	t = set_t(distance)

--- here velocity and cframes are calculated based on t

	Debris:AddItem(starclone, t+0.5)


	local ontouch = starclone.Touched:Connect(function(hit)
		local enemy = hit.Parent:FindFirstChild('Humanoid')
		if enemy == hum then return end
		if enemy and enemy ~= hum then
			enemy:TakeDamage(10)
		end
	end)
end)
Client/Tool
--Tool/ClientScript
local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()

local tool = script.Parent
local ammo = script.Parent.Ammo
local trigger = false

tool.Equipped:Connect(function()
	tool.Handle.Equip:Play()
end)

tool.Activated:Connect(function()
	if trigger == false then
		trigger = true
		tool.WeaponFired:FireServer(mouse.Hit.Position)
		wait(1)
		trigger = false
	end

end)
2 Likes

In this case, it might not have any difference on if you monitor the projectile on the server or client. This is because it appears you are using physics to send the projectile, which is networked to the client and server. However, you could probably set the network ownership to the client to provide a “smoother” throw.


you should detect collision on server, and then replicate projectile to every client

Could you please explain the collision part in more detail? Let’s say I render a projectile in a module script - how should I go about collision on server and connecting it to module?

1 Like

Okay, so I solved it thanks to this topic and ChatGPT…

  1. I’ve put Module in Replicated Storage, draw projectile in this module
  2. from client play this Module, sent remote to server on tool activated
  3. and then on the server, when the remote is activated, I put the very same remote to fire all clients…
    Correct me if I made any mistakes but so far it spawns projectiles nicely)))
    The next question is how to detect hits with server, but that is a theme for a different topic I guess…

collision detection can be raycast, and bullet can be fast part on client

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.