Why can't the script choose another item?

I am making a randomly generated obstacle course (often called an “obby”) and I am making three different levels, “L” for low, “M” for medium and “H” for high. The script can pick ladders that will increase the height, thats the problem.

If the script goes up to the highest height (“H”) I want it to not be able to pick ladders anymore, however the script seems to think differently.

here is the script:

local num = #script.Parent.levels:GetChildren()
local levels = script.Parent.levels
local items = game.ServerStorage.obby.hard:GetChildren()
local HML = "N"
local LMR = "M"
for i,v in pairs(levels:GetDescendants()) do
	if v:IsA("Part") then
		v.Transparency = 1
		v.CanCollide = false
	end
end

for i = 1,num,1 do
	randomitem = items[math.random(#items)] --items[math.random(1, #items)]:Clone()
	if HML == "H" and randomitem.way.Value ~= "up" then
		print(":(  ".. i)
		repeat
			randomitem = items[math.random(#items)]
			wait()
		until randomitem.way.Value == "neu"
	end
	local randomitemclone = randomitem:Clone()
	randomitemclone.Parent = workspace
	randomitemclone:SetPrimaryPartCFrame(levels[i][HML][LMR].CFrame)
	if randomitem.way.Value == "up" and HML ~= "H" then
		if HML == "L" then
			HML = "M"
		elseif HML == "N" then
			HML = "H"
		end
	end
print(i, HML)
	
end

This is the problem child:

repeat
	randomitem = items[math.random(#items)]
	wait()
until randomitem.way.Value == "neu"

It runs, the print right above it prints fine. But for some reason the script right above seems to think differently. Any help would be appreciated

2 Likes

I think the problem is that you didn’t give the math random a range but a straight max.
Should be 1,#items or esle #items will always he chosen

3 Likes

This is false.

When you assign only one value to math.random, it picks from 1-MaxAmount.

4 Likes

I am a bit uncertain why you have a repeat loop in the first place.

Could you specify a little better, what you’re trying to achieve?

1 Like

What sets randomitem.way.Value to "neu" so ladders don’t get placed above "H"?

1 Like

I have a repeat loop to pick another item that’s compatible with the path the script is making

1 Like

I do, I set the platform’s way to “neu” (short for neutral) so that it doesn’t change the height when it picks it. The ladders are set to the value of “up”, meaning that it will move the path up a block when a ladder gets chosen

1 Like

You did not quite specify what you’re trying to achieve in a better way

Try print(randomitem.way.Value) before the if randomitem line so you can see the actual Value.