Issue with procedural generation

  1. I want my procedural track generation to yield until replacement tracks have been made

  2. The issue is caused by more tracks trying to generate while it itself is regenerating


…causing track IDs to get skipped

Here’s what currently relates to the issue (regenerating, and how the iteration works)

Culprit code snippet #1

local hitbox = chosenTrack:FindFirstChild("Hitbox") --BasePart
local touching = hitbox:GetTouchingParts()
hitbox.Touched:Connect(function() end) --not pretty but its needed for gettouchingparts
for _, i in touching do
	if i.Parent ~= chosenTrack or i.Parent ~= lastTracks[1] then
		chosenTrack:Destroy()
		workspace.generatedTracks[iteration-1]:Destroy()
		warn("Retrying.. Track #"..iteration.." intersected something")
		print("Regenerating as "..iteration-1)
		local newtr = {workspace.generatedTracks[iteration-2],-1}
		return newTrack:Generate(newtr,iteration-1)
	end
end

Culprit code snippet #2

local trackNumber = math.random(650,1700)
local trackScript = require(script.trackscript) --referring to the ModuleScript Culprit #1 is found in
local prevTrack = {workspace.starttrack,1}

local constants = game:GetService("ServerStorage").constantspawns

print("TRACKS: "..trackNumber)
local iteration = 0 --stored outside function
for i=1, trackNumber+1 do --trackNumber+1 to track when course should end, removed for debugging
	if prevTrack[2] ~= nil then
		iteration +=prevTrack[2] --add or subtract, depends what returns
		print("Generating track "..i)
	end
	wait(.1) --Issue occurs here, I can't figure out how to make it yield to respect the ModuleScript
	prevTrack = trackScript:Generate(prevTrack,iteration)
end

Any help relating to my issue would be greatly appreciated

After some more testing, I’ve come up with a solution of re-ordering the code and making the tracks set the entire iteration number and not add or subtract from it.
But a new issue has arisen.


image
(the striked out 14 was the result of a mistake in the printing code)
Speaking of code, here’s the code now

local hitbox = chosenTrack:FindFirstChild("Hitbox") --BasePart
		local touching = hitbox:GetTouchingParts()
		hitbox.Touched:Connect(function() end) --not pretty but its needed for gettouchingparts
		for _, i in touching do
			if i.Parent ~= chosenTrack or i.Parent ~= lastTracks[1] then
				chosenTrack:Destroy()
				workspace.generatedTracks[iteration-1]:Destroy()
				warn("Retrying.. Track #"..iteration.." intersected something")
				print("Regenerating as "..iteration-2)
				local newtr = {workspace.generatedTracks[iteration-2],-1}
				return newTrack:Generate(newtr,iteration-2)
			end
		end

Great. Thanks. Very helpful.
Learned a very valuable lesson from this: troubleshoot parts at a time and don’t ask the devforum.

1 Like

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