How do I stop this from repeating?

Hello! I made this terrain generation system but I’m having this issue where when I’m changing biomes in the terrain, it prints this twice, causing a noticable notch/variation in the terrain…

10:48:12.219  Change biomes!  -  Server - Terrain:108
  10:48:12.219  Desert  -  Server - Terrain:111
  10:48:12.300  Change biomes!  -  Server - Terrain:108
  10:48:12.300  Mountains  -  Server - Terrain:111

Here’s the code, yes I tried doing a debounce but it doesn’t work.

	local debounce = true
			if x % BiomeSize == 0 and z % BiomeSize == 0 then -- In this example it will pause after each 100 loops
				spawn(function()
					if debounce == true then
						debounce = false
						print("Change biomes!")
						PrintScaleFactor = true
						currentBiome = Biomes[math.random(1, #Biomes)]
						print(currentBiome)
						wait(5)
						ScaleFactor = false
						debounce = true
					end
				end)
			end
1 Like

What is the reason for using spawn? This will complicate any analysis of your code because it has multiple threads.

local x = true
local ScaleFactor = true
local PrintScaleFactor = false
local Biomes = {"aaa", "bbb", "ccc"}
local currentBiome = "abc"

------
local debounce = true
if x then -- In this example it will pause after each 100 loops
	spawn(function()
		if debounce == true then
			debounce = false
			print("Change biomes!")
			PrintScaleFactor = true
			currentBiome = Biomes[math.random(1, #Biomes)]
			print(currentBiome)
			wait(5)
			ScaleFactor = false
			debounce = true
		end
	end)
end

Only prints once … Somehow, you’re calling this more than once while x is still true.
The spawn(function() took the following wait() out of consideration to the rest of the program also.

if debounce == true then debounce = false
	if x then -- In this example it will pause after each 100 loops
		spawn(function()
			print("Change biomes!")
			PrintScaleFactor = true
			currentBiome = Biomes[math.random(1, #Biomes)]
			print(currentBiome)
			wait(5)
			ScaleFactor = false
			debounce = true
		end)
	end
end

Maybe …depends on how this is called in the script.

turns out i need to put debounce at the beginning of the script, otherwise it just creates a new debounce every loop.

i used spawn because i wanted to delay the debounce from turning back on without having to worry about delaying the rest of the thread.

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