I am trying to make bandits spawn at certain positions in the Workspace and it is currently not working, there is a problem on line 7, in which the output says:
21:31:42.834 - Workspace.Script:22: attempt to call a table value
I also believe line 13 may result in an error but I am not sure because I haven't got that far through without it breaking, there are 3 parts in the Workspace called Spawn_Postion1
, Spawn_Postion2
and Spawn_Position3
This is a normal script in the Workspace
Colours = {BrickColor.new("Insitutional white"), BrickColor.new("Pastel yellow"), BrickColor.new("Sand red")} function Bandits() for i = 1, 3 do Bandit = game.Lighting.Bandit:Clone() Bandit.Name = "Bandit"..i for i in (Bandit:GetChildren()) do Choice = math.random(1, 3) if i.Name == "Left_Arm" or i.Name == "Right Arm" or i.Name == "Head" then i.BrickColor = Colours[Choice] end end Bandit.Torso.Position = game.Workspace[("Spawn_Position"..i)].Position end end
Thank you in advance
The problem IS line 7. Mainly, that isnt a for loop, numeric or generic or otherwise.
the :GetChildren() method provides an example with for loops
local children = workspace:GetChildren() for i = 1, #children do print(i, children[i].Name) end
but recommends
for index, child in pairs(workspace:GetChildren()) do print(index, child.Name) end
In your situation, it should be
function Bandits() for i = 1, 3 do Bandit = game.Lighting.Bandit:Clone() Bandit.Name = "Bandit"..i for i, _ ipairs(Bandit:GetChildren()) do Choice = math.random(1, 3) if i.Name == "Left_Arm" or i.Name == "Right Arm" or i.Name == "Head" then i.BrickColor = Colours[Choice] end end Bandit.Torso.Position = game.Workspace[("Spawn_Position"..i)].Position end end
You get that error because you're treating a table the same as an iterator function. Just use a true function, like pairs
or ipairs
.
for index, value in ipairs(Bandit:GetChildren()) do