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)