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)