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

Why is GetChildren not selecting things from my list?

In my game, there is a model called OilTanks with several models in it. This script is supposed to get the children of OilTanks, select a random model, and find a UnionOperation named Tank in the randomly selected model.

repeat
	wait(1)
	c = game.Workspace.OilTanks:GetChildren()

for l = 1, #c do
	local b = math.random(1, #c[l].Name) -- chooses a random tank

for i = 1, #b do
	if (b[i]:FindFirstChild("Tank") ~= nil) then
		print("Success")
		wait(3)
		script.MaxNumber.Value = script.MaxNumber.Value - 1
		wait(5)
wait()
end
end
end
until script.MaxNumber.Value == 0

The problem is that I get an error: "attempt to get length of local 'b' (a number value)" pertaining to "for i = 1, #b do" Also, would it be better if I just named the models to be randomly selected "Model1", "Model2"?

  • Workspace
    • OilTanks
      • Model
        • Tank
      • Model
        • Tank
      • Model
        • Tank

1 comment

0

What the heck? Who downvoted my question and someone's answer for no reason?

2 answers

✓ Accepted answer

The truth is, you're misunderstanding how the code works.

repeat
    wait(1)
    c = game.Workspace.OilTanks:GetChildren()

local b = c[math.random(#c)] -- A random tank
for i = 1, #b do
    if (b[i]:FindFirstChild("Tank") ~= nil) then
        print("Success")
        wait(3)
        script.MaxNumber.Value = script.MaxNumber.Value - 1
        wait(5)
wait()
end
end
end
until script.MaxNumber.Value == 0

What changed?
Before, you were selecting a random number (And not really properly at that), but now you are selecting a random number and using it as the key for the accessor in c

6 comments

0

Now it's firing an "attempt to get length of local 'b' (a userdata value)" error on the same "for i = 1, #b do"

0

Oh no it doesn't don't be silly. I explicitly made sure there's no way that can be true.

0

b will be an instance, which doesn't have a length. The original code didn't make much sense, and this sort of fixes it, but still not right

0

Never mind, I asked a friend for help. Apparently the for loop in line 6 wasn't needed, and line 5 should be "local b = c[math.random(1,#c)]"

0

Yeah my mistake on the length problem. Too much time with Valkyrie. As for line 5, there's nothing wrong with line 5

The you provided doesn't seem to do at all what you described.

You want to pick a random OilTank, and if it has an object called "Tank" in it, then 'count' it.

while script.MaxNumber.Value ~= 0 do
	local allTanks = workspace.OilTanks:GetChildren()
	local randomTank = allTanks[ math.random(#allTanks) ]
	if randomTank:FindFirstChild("Tank") then
		script.MaxNumber.Value = script.MaxNumber.Value - 1
		wait(8)
	end
end

Tab your code correctly! The above also includes some minor style corrections: workspace instead of game.Workspace; the 1 is optional in math.random; ~= nil and parens around a condition are both redundant.


Though, this doesn't make a whole lot of sense to do. You'll "repick" models all the time, and there's no reason to do this strange sort of counting, since you likely won't grab every tank (since it's random) and you'll probably repick some tanks (since it's random)

1 comment

0

I got a friend to help me out. Thanks anyway.

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