In pairs not working properly

im attempting to make clouds/waves with perlin Nosie and my in pairs only affects one part for some reason and i cannot figure out why

my code:

local X = 100
local Z = 100
local g = {}
yscale = 5
local seed = math.random(-100000,100000)
local tab = {}
game:GetService('RunService').Heartbeat:Connect(function()
	seed = math.random(-100000,100000)
end)
for x = 1, X do
	g[x]= {}
	for z = 1, Z do
		g[x][z] = math.noise(x/20, z/20,seed,-seed) * yscale 
	end
end


for x = 1, X do
	for z = 1, Z do
		local ypos = g[x][z]
		p = Instance.new('Part')
		table.insert(tab,p)
		p.Parent = workspace
		p.Transparency = g[x][z]
		p.Anchored = true
		p.Size = Vector3.new(1,1,1)
		p.Position = Vector3.new(x,0,z)
	end
end

while game:GetService("RunService").Heartbeat:Wait() do
	for x = 1, X do
		g[x]= {}
		for z = 1, Z do
			g[x][z] = math.noise(x/10, z/10,seed,-seed) * yscale 
		end
	end
	for _,v in pairs(tab) do
		for x = 1, X do
			for z = 1, Z do
			v.Transparency = g[x][z]
			v.Name = "Nil" -- Self Debugging 
				wait()
			end
		end		
	end
end

Could you show a video?

1 Like

Try merging your two heartbeat loops into 1, and see if that changes anything (probably wont). But do this to clean up the code and find the issue faster

Yea still the same thing, only changes the one block

Is it iterating through all 10000 blocks?

Yes its supposed to be doing that. But it only affects one

Could it be that your for loop isn’t adding the values correctly? Try printing the value of tab before going into the in pairs loop.

It inserts them all if i do print(#tab) it prints 1000 but the for loop only goes through one of them for some reason

Try applying a color to each part at the beginning of the pairs loop just to see if it correctly loops through each part.

Referring to
	for _,v in pairs(tab) do
		v.Color3 = Color3.fromRGB(3, 252, 44)
		for x = 1, X do
			for z = 1, Z do
			v.Transparency = g[x][z]
			v.Name = "Nil" -- Self Debugging 
				wait()
			end
		end		
	end

yea i have i do it with transparency it only affects one part, it doesnt even go through the table it only does the first one

The reason I am saying that you add it to the beginning is because it won’t be affected by the other for loops that happen in between (that set transparency). That way, if only one part changes color, it means that all 1000 parts in the tabs table are either the same part or duplicates of one.

Also, why are you looping through every transparency on each value? I could be wrong, but wouldn’t it be best if you just used their position as X and Z? I could be wrong, but I feel as if it has something to do with the fact that you loop through every transparency possible for every part that has been created.

It seems to me what you’re saying in the final

while game:GetService("RunService").Heartbeat:Wait() do
	...
	for _,v in pairs(tab) do
		print(_)
		for x = 1, X do
			for z = 1, Z do
				v.Transparency = g[x][z]
				v.Name = "Nil" -- Self Debugging 
				wait()
			end
		end		
	end
end

Is: "For every part v in tab,

Iterate through x 100 times
Iterate through z 100 times

Change v’s transparency to g[x][z]

So basically, you change v’s transparency 10,000 times. Then you do that to part 2. And part 3. And so on.

I think you want to instead find some way to iterate through each part and extract the proper transparency value from there. :slight_smile:

I can make a little diagram of what im trying to do if you want, it would probably be better for explanation

local X = 100
local Z = 100
local g = {}
yscale = 5
local seed = math.random(-100000,100000)
local tab = {}
game:GetService('RunService').Heartbeat:Connect(function()
	seed = math.random(-100000,100000)
end)
for x = 1, X do
	g[x]= {}
	for z = 1, Z do
		g[x][z] = math.noise(x/20, z/20,seed,-seed) * yscale 
	end
end


for x = 1, X do
	for z = 1, Z do
		local ypos = g[x][z]
		p = Instance.new('Part')
		table.insert(tab,p)
		p.Parent = workspace
		p.Transparency = g[x][z]
		p.Anchored = true
		p.Size = Vector3.new(1,1,1)
		p.Position = Vector3.new(x,0,z)
	end
end

function getPlaceFromIndex(index)
	local z = ((index - 1) % (Z) + 1) -- Should produce something between 0 and 99
	local x = math.ceil(index / X) -- Should produce something between 0 and 99
	return g[x][z]
end

while game:GetService("RunService").Heartbeat:Wait() do
	for x = 1, X do
		g[x]= {}
		for z = 1, Z do
			g[x][z] = math.noise(x/10, z/10,seed,-seed) * yscale 
		end
	end
	for i,v in pairs(tab) do
		local transparency = getPlaceFromIndex(i)
		v.Transparency = transparency
	end		
end

I tried to implement from your description… is this not what you’re going for?

1 Like

Yea this is almost exactly what i wanted!
image

my original goal was to make slowly shifting clouds

I think if you want shifting clouds, you need to keep the seed constant (otherwise you’re just changing the generation of clouds). Are you wanting them to wrap around or just go “off screen”?

Is this the effect you’re looking for? :slight_smile:
https://gyazo.com/172a24001eaa55b330868b0519e3664c

Could i see the code for this, I’d love to see how to did it! :sweat_smile:
And not particularly, I meant for the clouds to be more “Ungulating” in size