Server sided projectiles

Hello!

I’m currently trying to make a tower defense style game and one of the problems I ran into is dealing with projectiles and hit registration.

I’m using a heartbeat loop in the server to change the projectile’s CFrame and using GetTouchingParts in the same loop for hit reg. However, the projectiles are somewhat noticeably choppy when I click play to test. Interestingly though, everything looks perfectly smooth when I click run instead of play, so I’m assuming the problem is the frame differences between the server and the client.

How would I go about solving this issue? I’d prefer to keep everything server-sided for gameplay reasons (damage isn’t instant, accurate hit reg, etc.) but if there’s a viable option that involves the client, I’m open to trying it. I’m also not sure if using GetTouchingParts is the best option for hit reg, so is there a more efficient way to do this?

Here’s the full script I put inside the bullet and a clip of the basic layout of everything, although it might be hard to tell that the projectiles are choppy in the recording.

local runService = game:GetService("RunService")
local bullet = script.Parent
local currentPos = 0

local function GetTouchingParts(part)
	local connection = part.Touched:Connect(function() end)
	local results = part:GetTouchingParts()
	connection:Disconnect()
	return results
end

runService.Heartbeat:Connect(function()
	local startingCFrame = bullet.ShootCFrame.Value
	local studsPerFrame = bullet.SPF.Value
	if script.Parent ~= nil then
		local offsetCFrame = CFrame.new(0, 0, -currentPos)
		local touchingParts = GetTouchingParts(script.Parent)
		bullet.CFrame = startingCFrame:ToWorldSpace(offsetCFrame)
		currentPos += studsPerFrame
		for _, hit in pairs(touchingParts) do
			if hit.Name == "Cube" then
				hit.Health.Value -= 1
				bullet:Destroy()
			end
		end
	end
end)

Server-Sided projectiles are almost always going to look choppy in a live game. It’s pretty much unavoidable. The only way to guarantee smooth projectiles is to simulate them on the client.

For collision detection you usually want the client to detect the collision to avoid latency and then have the server verify that the collision was actually possible. If the collision was verified by the server then you can make it destroy the object it hit or whatever you want it to do when it gets hit

1 Like

Dont make it server sided it will be laggy try using this module fast cast it will simulate it in the client and send it in the server Making a combat game with ranged weapons? FastCast may be the module for you!

1 Like

Thanks for the feedback, for client hit reg though, wouldn’t the outcomes be slightly different for every client due to ping differences and what not? That could become a problem

1 Like

I’m aware that fastcast exists but I would prefer to make everything myself for this project, thanks anyways

1 Like

your gonna have to run it on the client if you don’t want choppy firerates, but you can run it through the server and client so everyone can see it.

I suggest having the projectiles load on the client, however the hit registration on the server, you could use remote events to do this.