DrawerModule Doesn't Work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am trying to make a module that I can use to make drawers functional, since I realized I will be needing this alot. I am also trying to make any contents inside the drawer move along with the drawer, so that I don’t have to attach it with a seperate script and create a mess.

  1. What is the issue? Include screenshots / videos if possible!

When I click on the drawer to make it open by changing the position, the drawer doesn’t move, even though the prints claim that it is working. No errors either.

local DrawerUsage = {}

--In order for this to work, you need to insert a click detector inside the drawer and define it in the parameters.
--Make sure to also define which drawer needs the script.
--The drawer HAS to be an union. The contents inside do not.

function DrawerUsage.OpenDrawer(X,Y,Z,Drawer,ClickDetector) -- X,Y,Z is how much the drawer should move
	print("function actually called")						--Drawers need to move 3.4 studs in order to fully open
	local opened = false
	print("Activated Module Function")
	local ContentsInsideDrawer = Drawer:GetChildren()
	ClickDetector.MouseClick:Connect(function()
		print("Clicked")
		local AClickDetector --This is here because I was planning to make the module check for the ClickDetector. When I did, the results were the same as this situation.
		if opened == false then
			Drawer.CFrame = Drawer.CFrame + Vector3.new(X,Y,Z) -- Opening the drawer with paremeters X,Y,Z
			print("Drawer Moved")
			if #ContentsInsideDrawer > 1 then --If things are inside the drawer, move all the contents with the drawer. I use more than 1 instead of 0 because a ClickDetector is in there
				for i,v in pairs(ContentsInsideDrawer) do --Formula needs to be setting the contents position to the same as the drawer except you need to subtract the content's position with the drawer's position
					local ContentOffset = v.Vector3 - Drawer.CFrame
					v.Vector3 = Drawer.CFrame + ContentOffset
				end
			end
			opened = true
		end
		if opened == true then
			Drawer.CFrame = Drawer.CFrame - Vector3.new(X,Y,Z) -- Closing the drawer with parameters X,Y,Z
			if #ContentsInsideDrawer > 1 then
				for i,v in pairs(ContentsInsideDrawer) do
					local ContentOffset = v.Vector3 - Drawer.CFrame -- The contents will follow alongside the drawer.
					v.Vector3 = Drawer.CFrame + ContentOffset
				end
			end
			opened = false
		end
	end)
end

return DrawerUsage

Output:

function actually called

Activated Module Function

Clicked

Drawer Moved

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

On one of my attempts of fixing this module, I tried to set if #ContentsInsideDrawer > 0 then to see what would happen, and the drawer ended up actual opening, but when I clicked the drawer again to close it, the drawer just kept repeating the “Open Drawer” sequence, because an error popped up telling me that ClickDetector doesn’t have a CFrame/Vector3, so therefore, it could not continue running the module.

Please let me know if there is a better way to implement this module (e.g. less parameters). Thanks!

I tested your module, and It seems like your module isn’t working, because the drawer opens and immediately closes after. You can use return without filling anything after to just stop the code right there. (Basically a break for functions)

1 Like

Wow, thanks! It actually worked! You’re a legend :smiley: (cough I wish) I fixed some bits of my code after adding in the returns, and now everything is functioning. (I said I would make another topic but I managed to fix it)

No problem, man! Just make sure to fix your code up next time, because it looks kind of messy, and I could totally see a better way to do this.

Excuse me but you guys are same person right?
mhm. @Galaxy_Guest already solved it mhm.

1 Like