My function keeps firing constantly (ShootRockets()) even when i put a task.wait

I want the ShootRockets function to only fire every 3 seconds using task.wait(3), but for some reason it fires constantly and completely ignores the task.wait. Also when i leave the detection radius for the killbot, the ShootRockets function keeps firing
even when it doesn’t meet the condition of detecting a player. Can anyone help? Any is greatly apreciated, thank you

Also, if it helps, this is in a serverscript.

-- local KillBot = script.Parent

local ScanRadius = 50

local HumanFound = false

local Sounds = {
	["TargetLock"] = script.Parent.TargetLock,
	["Random"] = script.Parent.Random,
	["Rocket"] = script.Parent.Rocket,
}

local function Roam()
	
	local TargetLocation = KillBot.Position + Vector3.new(math.random(-10, 10),math.random(-10, 10),math.random(-10, 10))
	
	KillBot.CFrame = CFrame.lookAt(KillBot.Position, TargetLocation)
	
	repeat
		KillBot.CFrame *= CFrame.new(0,0,-0.01)
		task.wait()
	until KillBot.Position == TargetLocation
	
	task.wait(math.random(1, 5))
end

local SoundDebounce = false

local function ScanForHumans()
	for i, player in pairs(game.Players:GetChildren()) do
		if player.Character then
			local Character = player.Character
			
			local Distance = (KillBot.Position - Character.HumanoidRootPart.Position).Magnitude

			if Distance <= ScanRadius then
				HumanFound = true
				
				if SoundDebounce == false then
					Sounds.TargetLock:Play()
					SoundDebounce = true
				end
				
				KillBot.CFrame = CFrame.lookAt(KillBot.Position, Character.HumanoidRootPart.Position)
				KillBot.CFrame *= CFrame.new(0,0, -0.050)
			end
		else
			HumanFound = false
			SoundDebounce = false

			while HumanFound == false do
				Roam()
				task.wait()
			end
		end
	end
end

local function ShootRockets()
	local New = game.ReplicatedStorage.Objects.Rocket:Clone()
	New.Parent = workspace
	New.CFrame = KillBot.CFrame * CFrame.new(0,0,-2)
	task.wait(3)
end

local function Hover()
	local RayOrigin = KillBot.Position
	local RayDirection = Vector3.new(0, -10, 0)

	local Raycast = workspace:Raycast(RayOrigin, RayDirection)

	if Raycast then
		KillBot.CFrame *= CFrame.new(0,0.1,0)
	end
end

game:GetService("RunService").PreSimulation:Connect(function() --- This is where the problem starts
	Hover()
	
	ScanForHumans()
	
	if HumanFound then
		ShootRockets() <-
	end
	
end)


the function is connected to the runservice onstepped event, which means it will get called each frame

And then at the end it waits 3 seconds, but each call is in a separate thread, which doesnt delay the next time the event is fired. So the 3 second wait does nothing

Instead just change your loop to one that runs every 3 seconds and dont put wait in the function (generally bad idea)

-- in a separate loop, because Hover still has to run every frame
while true do
  ScanForHumans()
  if HumanFound then
    ShootRockets()
  end

  task.wait(3)
end
1 Like

thank you so much bro!

(i have to fit the character limit don’t mind this)

or it can be

local shooting_rockets = false
game:GetService("RunService").PreSimulation:Connect(function()
	if shooting_rockets then return end
	shooting_rockets = true
	Hover()
	
	ScanForHumans()
	
	if HumanFound then
		ShootRockets()
	end
	shooting_rockets = false
end)
1 Like

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