[SOLVED] How to make a randomly generating drawer system?

Hey there, I am making a game where you can explore the map and there are drawers which I want to spawn items randomly, but while making sure each item spawns at least once in one of the drawers.

So there is 5 drawers around the map, and when you open one it will choose either Example1, Example2 or Example3 to appear for you to grab. I want to make sure Example1, Example2 and Example3 spawns at least one time in one of the drawers. But with the current system, it is possible for Example1 to appear in all of them, while Example2 and Example3 won’t even appear once.

My current system is:

local frame = script.Parent
local openSound = frame:WaitForChild("DoorOpen")
local model = frame.Parent
local proximityPrompt = model.Handle.ProximityPrompt
local frameOpen = model:WaitForChild("DoorFrameOpen")
local tweenService = game:GetService("TweenService")

 
proximityPrompt.Triggered:Connect(function()
	proximityPrompt.Enabled = false
	
	-- Choosing Item
	local random = math.random(1,3)
	if random == 1 then
		item = model.DoorFrameOpen.Items.Example1
	elseif random == 2 then
		item = model.DoorFrameOpen.Items.Example2
	else
		item = model.DoorFrameOpen.Items.Example3
	end
    openSound:Play()
	tweenService:Create(frame,TweenInfo.new(.80),{CFrame = frameOpen.CFrame}):Play()
	item.Transparency = 0
	item.ProximityPrompt.Enabled = true
end)

Sort of found a solution:

local drawers = script.Parent.Drawers

local function decideExample1()
	local random = math.random(1,3)
	if random == 1 then
		if drawers.SmallDrawer1.Item.Value == "NotSet" then
			print("E1 Drawer1")
			drawers.SmallDrawer1.Item.Value = "Example1"
		else
			decideExample1()
		end
	elseif random == 2 then
		if drawers.SmallDrawer2.Item.Value == "NotSet" then
			print("E1 Drawer2")
			drawers.SmallDrawer2.Item.Value = "Example1"
		else
			decideExample1()
		end
	else
		if drawers.SmallDrawer3.Item.Value == "NotSet" then
			print("E1 Drawer3")
			drawers.SmallDrawer3.Item.Value = "Example1"
		else
			decideExample1()
		end
	end
end

local function decideExample2()
	local random = math.random(1,3)
	if random == 1 then
		if drawers.SmallDrawer1.Item.Value == "NotSet" then
			print("E2 Drawer1")
			drawers.SmallDrawer1.Item.Value = "Example2"
		else
			decideExample2()
		end
	elseif random == 2 then
		if drawers.SmallDrawer2.Item.Value == "NotSet" then
			print("E2 Drawer2")
			drawers.SmallDrawer2.Item.Value = "Example2"
		else
			decideExample2()
		end
	else
		if drawers.SmallDrawer3.Item.Value == "NotSet" then
			print("E2 Drawer3")
			drawers.SmallDrawer3.Item.Value = "Example2"
		else
			decideExample2()
		end
	end
end

local function decideExample3()
	local random = math.random(1,3)
	if random == 1 then
		if drawers.SmallDrawer1.Item.Value == "NotSet" then
			print("E3 Drawer1")
			drawers.SmallDrawer1.Item.Value = "Example3"
		else
			decideExample3()
		end
	elseif random == 2 then
		if drawers.SmallDrawer2.Item.Value == "NotSet" then
			print("E3 Drawer2")
			drawers.SmallDrawer2.Item.Value = "Example3"
		else
			decideExample3()
		end
	else
		if drawers.SmallDrawer3.Item.Value == "NotSet" then
			print("E3 Drawer3")
			drawers.SmallDrawer3.Item.Value = "Example3"
		else
			decideExample3()
		end
	end
end

decideExample1()
wait(1)
decideExample2()
wait(1)
decideExample3()

My only issue with this, is that it could take a while to generate once I add more than 5 drawers to the map, but I guess I will just have to use this.

Alright found a better solution, and I am just going to post it if anyone else wants it.

It utilizes tables, and removing it and now doesn’t give any errors, and it works fast.

local drawers = script.Parent.Drawers
local cabins = script.Parent.Cabins

local cabinTable = {"Cabin1", "Cabin2", "Cabin3"}
local drawerTable = {"DrawerOne", "DrawerTwo", "DrawerThree", "DrawerFour", "DrawerFive", "DrawerSix"}



local function chooseExample1()
	local chosen = drawerTable[math.random(1, #drawerTable)]
	local numberPosition = table.find(drawerTable, chosen)
	print("Example1: "..chosen)
	table.remove(drawerTable, numberPosition)
	if chosen == "DrawerOne" then
		drawers.SmallDrawer1.Item.Value = "Example1"
	elseif chosen == "DrawerTwo" then
		drawers.SmallDrawer2.Item.Value = "Example1"
	elseif chosen == "DrawerThree" then
		drawers.SmallDrawer3.Item.Value = "Example1"
	elseif chosen == "DrawerFour" then
		drawers.SmallDrawer4.Item.Value = "Example1"
	elseif chosen == "DrawerFive" then
		drawers.SmallDrawer5.Item.Value = "Example1"
	elseif chosen == "DrawerSix" then
		drawers.SmallDrawer6.Item.Value = "Example1"
	end
	
end
1 Like