Randomly generated room item spawns with seeds unable/very unlikely to get a median value

I have code to generate spawns for items with a seed, so if a player encounters a impossible situation, I can easily recreate it. But it cannot generate median numbers. Here is the code:

			for _i, item:Model in pairs(chapter.traverseCritItems[traverseroom.Name]:GetChildren()) do
				local index = 0
				local previousindex = 0
				local indexcap = #chapdata.traversecrititemspawns[traverseroom.Name]
				local itemspawn = nil
				local fakecount = 0
				for count = 1,traverseseed,1 do
					fakecount += 1
					index = math.clamp(fakecount,1,indexcap)
					print("index: "..index.." ".."previousindex: "..previousindex.." ".."indexcap: "..indexcap.." ".."origcount: ".." "..count)
					if index > indexcap or previousindex == indexcap then
						previousindex = 0
						index = 1
						fakecount = 0
						print("resettedindex: "..index.." ".."resettedpreviousindex: "..previousindex.." ".."indexcap: "..indexcap.." ".."origcount: ".." "..count)
					end
					task.wait()
					previousindex = index
					itemspawn = chapdata.traversecrititemspawns[traverseroom.Name][index]
				end
				if not table.find(crittakenspawns,itemspawn) then
					print("Spawn Open!")
					table.insert(crittakenspawns,itemspawn)
					item:PivotTo(itemspawn)
					item.Parent = traverseroom
				else
					print("Spawn taken, getting new one")
					fakecount = 0
					local attempts = 10
					repeat 
						fakecount += 1
						attempts -= 1
						index = math.clamp(fakecount,1,indexcap)
						print("index: "..index.." ".."previousindex: "..previousindex.." ".."indexcap: "..indexcap.." ".."origcount: ".." "..fakecount)
						if index > indexcap or previousindex == indexcap then
							previousindex = 0
							index = 1
							fakecount = 0
							print("resettedindex: "..index.." ".."resettedpreviousindex: "..previousindex.." ".."indexcap: "..indexcap.." ".."origcount: ".." "..fakecount)
						end
						task.wait()
						previousindex = index
						itemspawn = chapdata.traverseExtraItemspawns[traverseroom.Name][index]
					until not table.find(crittakenspawns,itemspawn) or attempts == 0
					if attempts == 0 then
						print("Not enough spawns, deleting item "..item.Name)
						item:Destroy()
					else
						print("Found new Spawn!")
						table.insert(crittakenspawns,itemspawn)
						item:PivotTo(itemspawn)
						item.Parent = traverseroom
					end
				end
			end

Here is the module that holds the chapter settings:

chapmodule.traversecrititemspawns = {
	traversetesting = {
		[1] = CFrame.new(109.21, 11.5, 142.37);
		[2] = CFrame.new(109.21, 6.86, 142.37);
		[3] = CFrame.new(109.21, 2.29, 142.37);
		[4] = CFrame.new(109.23, 10.61, 179.08);
		[5] = CFrame.new(109.23, 6.49, 179.08);
		[6] = CFrame.new(109.23, 2.44, 179.08)
	}
}

chapmodule.traverseExtraItemspawns = {
	traversetesting = {
		[1] = CFrame.new(117.3, 11.02, 144.38);
		[2] = CFrame.new(117.3, 7.03, 144.38);
		[3] = CFrame.new(117.3, 2.66, 144.38);
		[4] = CFrame.new(117.3, 2.66, 181.09);
		[5] = CFrame.new(117.3, 6.47, 181.09);
		[6] = CFrame.new(117.3, 11.07, 181.09)
	}
}

Thanks in advance! This has been bugging me for weeks. Literally pun intended

2 Likes

Can you better describe what you mean by a median value?

1 Like

Like in the chapter data table, there is 1,2,3,4,5,6? the median would be 3 or 4. I should phrase this better, but essentially the system cant pick any spawn, whos index is a median value. @PuffoThePufferfish Thanks!

If you’ve narrowed it down to the index being the problem then I reckon it shouldn’t be too difficult to debug with a bit of printing and logic-following; What is traverseseed defined as?

1 Like
local traverseseed = Random.new():NextInteger(1,44)

this. I think this is the prob ngl @PuffoThePufferfish

Also, thanks for responding to late replies. This is a big problem for me

This whole codebase seems very convoluted and overcomplicated; I can’t comprehend what your end goal is, maybe try to implement code stylization and de-nesting. I find CodeAesthetic’s videos to be helpful with these, but for extra information, use the search terms “code formatting” and “style guide”

In order to get a “Random” object you would use Random.new(parameter) with the seed as the parameter. Leaving it blank generates an unknown & unknowable seed, which is equivalent to the superior math.random(1,44) (assuming math.randomseed wasn’t previously set)

If you can’t find your problem: I recommend just re-writing the code with clarity in mind, and clearly defining your path of logic both in the code using comments in-script as well as pseudocode/flowcharts before you write said code.

Ok so I will rewrite the code, but with better formatting. My goal is to have a random seed system to easily reproduce bad generation. I increment a index every iteration of the for count = 1,traverse seed do. If the index is bigger then the amount of spawns in the chapter settings, or the previous index is equal to the amount of spawns, it will reset the index. And at the end of the count loop, it will look at the item spawns, and use the index to get a spawn. @PuffoThePufferfish

This is my new code, with comments:

		if traverseroom.Name == boundfolder.Name then
			print("traverseloadingstarted")
			local crittakenspawns = {}
			local extratakenspawns = {}
			local traverseseed = randomseed:NextInteger(1,44)
			local traverseindict = Instance.new("IntValue")
			traverseindict.Name = "seed"
			traverseindict.Value = traverseseed
			traverseindict.Parent = traverseroom
			for _,item:Model in pairs(chapter.traverseCritItems[traverseroom.Name]:GetChildren()) do --loop through all items
				local index = 0 --spawn index
				local previousindex = 0 --previous spawn index
				local itemspawn:CFrame = nil ---item spawn for item
				local indexcap = #chapdata.traversecrititemspawns[traverseroom.Name] --the max number index can go to
				
				for c = 1,traverseseed,1 do -- repeat traverse seed times
					index += 1 -- increment index
					index = math.clamp(index,1,indexcap) --clamp theindex
					print("index: "..index.." ".."previousindex: "..previousindex)-- debuging stuff
					if index > indexcap or previousindex +1 == indexcap then -- if the index is bigger then the index cap, or previous index +1 is the index cap, reset both values
						index = 0
						previousindex = 0
					end
					previousindex = index --set the previous index
					itemspawn = chapdata.traversecrititemspawns[traverseroom.Name][index]--set the itemspawn
					task.wait()
				end
				if not table.find(crittakenspawns,itemspawn) then --if the spawn is in the taken spawn table, keep going in the loop
					if index == 0 then --if the loop last resetted it it will be 0, so switch it to one
						index = 1
						itemspawn = chapdata.traversecrititemspawns[traverseroom.Name][index]
					end
					item:PivotTo(itemspawn) --pivot it
					table.insert(crittakenspawns,itemspawn)
					item.Parent = traverseroom
				else -- continue the loop, basicly a cpoy+paste
					index = 0
					previousindex = 0
					local attempts = 10
					repeat
						attempts -= 1
						index += 1
						index = math.clamp(index,1,indexcap)
						print("index: "..index.." ".."previousindex: "..previousindex)
						if index > indexcap or previousindex +1 == indexcap then
							index = 0
							previousindex = 0
						end
						previousindex = index
						itemspawn = chapdata.traversecrititemspawns[traverseroom.Name][index]
						task.wait()
						
					until not table.find(crittakenspawns,itemspawn) or attempts == 0
					if (not attempts) == 0 then
						print("found new spawn for item "..item.Name)
						if index == 0 then --if the loop last resetted it it will be 0, so switch it to one
							index = 1
							itemspawn = chapdata.traversecrititemspawns[traverseroom.Name][index]
						end
						item:PivotTo(itemspawn)
						table.insert(crittakenspawns,itemspawn)
						item.Parent = traverseroom
					else
						print("Not enough spawns for all items, destroying")
						item:Destroy()
					end
				end
			end

Also, the random seed is defined as this:local randomseed = Random.new(2)

Should I just play around with the random seed?

I replaced random.new with math.random, and it worked alot better! thanks a whole lot!

1 Like

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