How do I implement a spawn feature in my tower?

Basically, I want to make my tower spawn a thing to attack enemies, like Military Base and Crook Boss from TDS. I have no idea how to do this. Here’s my function. Tell me if you need a certain part of the script. btw, it attacks and is supposed to spawn at the same time, so more like crook boss

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)


	if target ~= nil and target:FindFirstChild("Humanoid") then
		AnimateEvent:FireAllClients(newTower, "Attack", target)
		if target then
			local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
			
			newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		end
		
		damage(target, config)
		
		abilities(newTower, config, target, player)

		task.wait(config.Cooldown.Value)
	end

	task.wait(0.05)

	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end
2 Likes

Can you maybe be specific or provide a video / image of what your trying to do. I don’t entirely understand what your trying to do, and it would be nice to have more details / Visuals for what your trying to do.

4 Likes

you can see this if this helps,

4 Likes

You will need to clone it

Script
local spawnedthing = thing:Clone()

Now you have cloned it and it was spawned in, this means you can have multiple at the same time and manage them too
spawnedthing is the new spawned item which you want to use.
thing is the original item that you want to spawn in

3 Likes

ok, i’ll keep that to remember, but also, how would it function? I want to put it inside the attack function, but it has a seperate cooldown, so it isn’t supposed to spawn when it attacks.

3 Likes
local function clone()
  local itemToClone = 
  itemtoClone -- make this the item you want to clone

  local newItem =  itemToClone:Clone()
  newItem.Parent = tower -- This is now the spawned item
end

Now run this function whenever you want clone the item

5 Likes

So I have this, but the wait(1) is interrupting the rest of the script in the attack function. How would I stop this?

	if config.OriginalTower.Value == "tower" then
		if config.SpawnCooldown.Value <= 0 then
			SpawnUnit(newTower)
		else
			wait(1)
			config.SpawnCooldown.Value -= 1
		end
	end
3 Likes

You could use a co-routine, it allows you to complete multiple functions at one: coroutine | Documentation - Roblox Creator Hub

Or

You could put this into another script

2 Likes

how would I intergrade that inside of my script? Sorry, I just haven’t worked with them much.

1 Like

I haven’t yet come across a time where I am in need of this but I did some studying and this is what you can do:

local action = coroutine.create(function()
	--Here put the function in where you clone the turret
end)

coroutine.resume(action) --This runs the co-routine which runs while the rest of the script is running, even with waits

--The rest of the script can go anywhere

This should hopefully work.

What you want to do is when you want to spawn unit, you put it into this coroutine and it should do it while the rest of the script is running

I’m not sure if this is what you want but (If not you can just change it):

local clone = coroutine.create(function()
	if config.OriginalTower.Value == "tower" then
		if config.SpawnCooldown.Value <= 0 then
			SpawnUnit(newTower)
		else
			wait(1)
			config.SpawnCooldown.Value -= 1
		end
	end
end)

coroutine.resume(clone) --When you want to check if you can clone the turret
1 Like

I mean it works, but when the tower isnt attacking, the spawning goes like 3x speed. Probably because its in a function that runs infinitely when the tower is spawned.

2 Likes

You could use a Boolean value for when you want it to run

2 Likes

It works, just one last problem, the usual tower cooldown is interrupting the spawn. I have a sollution in mind, but it makes the script extremely messy.

2 Likes

How is it interrupting it?
What is the messy script idea in mind?

1 Like

My idea is this:

wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret
wait(config.Cooldown.Value / 10)
coroutine.resume(spawnUnit) --When you want to check if you can clone the turret

It cant check while the cooldown is running

1 Like

Why not just have scripts to spawn the units and check if the tower exists every interval?

wouldnt a while loop do this more efficiently?

as i said, it makes the script messy, so it really wasnt supposed to be used.
Anyway I tried a for i loop, it didnt work.

Edit: It did work, just didnt look like it.

What didn’t work about it, I don’t know if it’s possible to put co-routines in co-routines

well, it did work, It just didnt look like it.

Welp, it works all good now! Thanks for all the help. And thanks to everyone else!

1 Like