Why is this loop behaving weirdly?

Hi Guys, so basically I have a loop and it’s behaving really weirdly.

Code -

local X = 200
local Z = 100
local previousOffset = 0

for x=1, X do
	for z=1, Z do
		print(previousNoise.." - Before") -- basically here, for some reason, it's printing 17500 and it's weird.
		local noise = math.noise(x/X, z/Z, 100) * 2
		local y = math.ceil(noise + previousNoise) * 4
		previousNoise += 1
		--print(y)
	end
end

So basically even at the start of the loop, it’s starting from 17,500 and I dont really know why. Any help is appreciated. Thanks :slight_smile:

Your code doesn’t show where the variable prevousNoise is being initialized, if your comment says it prints 17500 that that line, then obviously the only thing that could be 17500 is that variable.

Could you show us more of the code?

This is the only code inside the script and this is the one and only script in the game.

That’s impossible. You need to define it before calling it otherwise it will error.

1 Like

If that’s the case then why wouldn’t the script error? Because if you haven’t defined previous noise somewhere the script won’t know what it is…

1 Like

I just added rest of the code in comments.

The script won’t error because I have assigned the variable before as you could clearly see…

I know, that’s why I asked on the forums.

Now you fixed your code, now I see the problem. You forced the loop to execute itself instantly without waiting, which resulted in it creating tens of thousands of prints in an output that can only contain a few thousand of them - this is why the first print that you see says “17500” or in my case “15000”. Add a “task.wait()” after every ‘for do’ loop.

1 Like

It was always fixed though.

It was assigned here.

Anyway I’ll try this and see if it fixes.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.