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

Some i,v in pairs() behavior I dont understand?

Asked by
Wayyllon 170
3 years ago

So I'm trying to create a system where after selecting a random number it will set a value inside of a part and change its Color/Material

This is the code I have, and yes, I'm aware I have no clue what I'm doing.

GigaChunk = game.Workspace.GigaChunk
for i,v in pairs(GigaChunk:GetChildren()) do
    for i,v in pairs (GigaChunk:GetChildren()) do
        ChunkIteration = v
        for i,v in pairs (ChunkIteration:GetChildren()) do
            RandomNumber = math.random(1,3)
            if RandomNumber == 1 then
                local MineralValue = Instance.new("NumberValue")
                MineralValue.Value = 1
                MineralValue.Parent = v
                v.Color = Color3.fromRGB(200,100)
                v.Material = Enum.Material.Glass
                RandomNumber = math.random(1,3)
                wait()
            elseif RandomNumber == 2 then
                local MineralValue = Instance.new("NumberValue")
                MineralValue.Value = 2
                MineralValue.Parent = v
                v.Color = Color3.fromRGB(150,100)
                v.Material = Enum.Material.Slate
                RandomNumber = math.random(1,3)
                wait()
            else
                local MineralValue = Instance.new("NumberValue")
                MineralValue.Value = 3
                MineralValue.Parent = v
                v.Color = Color3.fromRGB(150,100)
                v.Material = Enum.Material.Metal
                RandomNumber = math.random(1,3)
                wait()
            end
        end
    end
end

This script works without the waits; However, Theres a few problems.

When I first ran this script, I didn't have the

-Number scramblers inside of the if statements & -wait() 's

When I first ran the script, it did it for all of the Chunks, but the only thing it was randomizing for was #2, and so all of the randomized parts were changed by the 2nd if statement.

(Another strange behavior is that only some of the parts were changed, some stayed as stone about a 50:50 ratio)

When I added the number scramblers inside of the if statements it didn't affect this problem.

When I run it without the wait() the script works, but it still says "Exhausted execution timeout" meaning i need to add a wait essentially, but when i do add the waits, nothing happens.

This is definitely user error as i do not fully understand for i, v in pairs loops.

(Another thing to note is that i also tried to put the wait inside the for i,v in pairs loop instead of having it nested inside of the if statements.)

However, if someone could explain where I'm making mistakes, it would be a great help.

Ps: If there's a way to get all the parts in the radius of a part please inform me.

Thanks For reading.

1 answer

Log in to vote
0
Answered by
Wayyllon 170
2 years ago
Edited 2 years ago

Thought i'd pop back in with a little more scripting knowledge god forbid anyone attempts to do this in the future.

First, if you wanna get the parts in a radius of another one, use :GetTouchingParts

Second, rgb values require three values, red green and blue, not two, crazy!

Ad

Answer this question