Stuck on interval for AI

Sorry, last post for today.

I am making an AI, where, after 5 seconds, they place a trap down.

Here is my code:

while wait(5) do
	local rStorage = game:GetService("ReplicatedStorage")
	local folder = rStorage:WaitForChild("Traps")
	local trap = folder:FindFirstChild("Trap")
	
	local trapCount = 5
	
	if trapCount > 0 then
			placeTraps(trap)
			trapCount = trapCount - 1
	end
end

Here is the placeTraps function:

local function placeTraps(trap)
	local clone = trap:Clone()
	clone.Parent = game.Workspace
	clone.Position = killer.HumanoidRootPart.CFrame - Vector3.new(0,2.5,0)
end

It doesn’t seem to be doing the interval, or placing the traps down at all.

Any help?

Any advice? I am not sure how I should format my code in order for this to work

Do you have another loop running for the NPC’s movement?

If the while loop isn’t even running, then there isn’t enough code here for us to really know why.

As far as your placeTraps function, i would change the position code:

You’re setting position (a Vector3 value) to a CFrame (a matrix) subracted by a vector3 value.

I would change that code to this:

clone.Position = killer.HumanoidRootPart.Position- Vector3.new(0,2.5,0)

I would also make sure that the -2.5 in the vector isn’t too much, i.e make sure the trap isn’t appearing in the floor.

1 Like

Yes, I have a while wait(1) do loop.

If you have 2 loops in 1 script they won’t both run.

1 Like

That makes sense.

How would I enable this to run then?

try using coroutine
heres a good video that explains them https://www.youtube.com/watch?v=f3bcZK7_F-E

1 Like

you can either use

spawn(function()
--loop here
end)

or

coroutine.resume(coroutine.create(function()
-- loop here
end))
1 Like

Made it work!

Just had to tinker with some functions and values etc…

Thanks for helping.