So I’m currently working on a tower defense game. I coded it so that the towers will search through a folder where zombies are stored, and target the closest one with a while true loop. It works pretty well. The only problem I’m having is that the firerate seems slightly slower than it should be, and sometimes it heavily delays the next shot. For example, a tower could be firing with a firerate of 0.1, but delays the next shot by 0.5 seconds for some reason.
You should have a loop that runs every tick (RunService.Stepped for example) and it should fire your projectile every time the firerate has passed, then subtract the firerate from the time. This way, you will never “lose” any time.
local totalTime = 0
local fireTime = 0.1
--Loop inside a Stepped function
function OnStep(time, deltaTime)
totalTime += deltaTime
if totalTime > fireTime
Fire()
totalTime -= fireTime
end