Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Problem with tables?

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

4 comments

0

Line 13 is fine. The output says line 22. Post the rest.

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.

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

0

You are getting this error because it is trying to call the table as an iterator function.

2 answers

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.