Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problem with tables?

Asked by
Mystdar 352 Moderation Voter
9 years ago

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

0
Line 13 is fine. The output says line 22. Post the rest. Shawnyg 4330 — 9y
0
Post the rest of the code; We DO NOT know what Line 22 is! Please post the full script so we can have a better understanding of the problem. TheeDeathCaster 2368 — 9y
0
There is no rest of code, that is all the code, when I start a new script I write each line(ish) out in commented psuedocode that is all the actual code, but I can twll you line 7 above is line 22 in the wcript Mystdar 352 — 9y
0
You are getting this error because it is trying to call the table as an iterator function. NotsoPenguin 705 — 9y

2 answers

Log in to vote
1
Answered by
iaz3 190
9 years ago

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
Ad
Log in to vote
1
Answered by 9 years ago

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

Answer this question