Hello! I can’t figure out why it only repeats once, and only spawns the “target” part. My brain might just not be working but I’m not sure. This is in a local script for the gui. Thanks!
local colors = {"Red", "Pink", "Yellow", "Blue", "Green", "Orange"}
local target = {}
local enemys = 5
local function spawnEnemy()
local selecttarget = colors[math.random(1, #colors)]
table.insert(target, 1, selecttarget)
print(target)
for i = 1, enemys do
local enemySpawn = colors[math.random(1, #colors)]
if table.find(target, enemySpawn) then
local enemySpawned = script.Parent.Parent.Targets:FindFirstChild(enemySpawn):Clone()
enemySpawned.Parent = script.Parent.PlayingArea
local posX = math.random(0.876, 0)
local posY = math.random(0.89, 0)
enemySpawned.Position = UDim2.new(posX, 0, posY, 0)
else
local enemySpawned = script.Parent.Parent.Targets:FindFirstChild(enemySpawn):Clone()
enemySpawned.Parent = script.Parent.PlayingArea
local posX = math.random(0.876, 0)
local posY = math.random(0.89, 0)
enemySpawned.Position = UDim2.new(posX, 0, posY, 0)
end
end
end
wait(10)
spawnEnemy()
You only ever call the function named “spawnEnemy” once. To call it multiple times you could use the following.
local colors = {"Red", "Pink", "Yellow", "Blue", "Green", "Orange"}
local target = {}
local enemys = 5
local function spawnEnemy()
local selecttarget = colors[math.random(1, #colors)]
table.insert(target, 1, selecttarget)
print(target)
for i = 1, enemys do
local enemySpawn = colors[math.random(1, #colors)]
if table.find(target, enemySpawn) then
local enemySpawned = script.Parent.Parent.Targets:FindFirstChild(enemySpawn):Clone()
enemySpawned.Parent = script.Parent.PlayingArea
local posX = math.random(0.876, 0)
local posY = math.random(0.89, 0)
enemySpawned.Position = UDim2.new(posX, 0, posY, 0)
else
local enemySpawned = script.Parent.Parent.Targets:FindFirstChild(enemySpawn):Clone()
enemySpawned.Parent = script.Parent.PlayingArea
local posX = math.random(0.876, 0)
local posY = math.random(0.89, 0)
enemySpawned.Position = UDim2.new(posX, 0, posY, 0)
end
end
end
while wait(10) do
spawnEnemy()
end
This will call the function every 10 seconds, assuming that is how frequently you want the function to be ran.
You’re inserting an entry to the same table index each time, which means that entry which was previously located at that index is overridden, is this intentional?