Help with Grid Inventory

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 want it to not detect the collisions that are empty. It still thinks that the item is at that location after scrolling down the scrollingframe. I don’t know how to make the position move with the scrollingframe. I tried many times already.

  1. What is the issue? Include screenshots / videos if possible!
    Imgur: The magic of the Internet

  2. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    ChatGPT

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I am using GridPack: GridPack - Create Grid/Tetris Style Inventories . It does not work with ScrollingFrames, and I am trying to make so it does.

Here is the code that is part of the module that detects the drop and collision. I don’t know what to change/adjust to make it work.

self._trove:Add(UserInputService.InputEnded:Connect(function(input)
		-- Drop item when left mouse stops getting clicked
		if input.UserInputType == Enum.UserInputType.MouseButton1 and self.IsDragging == true and self.ItemManager ~= nil then
			self.IsDragging = false

			-- Check if the item is colliding, if not add the item to the itemManager
			local currentItemManager = self.HoveringItemManager or self.ItemManager
			
			local gridPos = currentItemManager:GetItemManagerPositionFromAbsolutePosition(self.ItemElement.AbsolutePosition, self.Size, self.PotentialRotation)

			local rotatedSize = self.Size
			if self.PotentialRotation % 2 == 1 then
				rotatedSize = Vector2.new(self.Size.Y, self.Size.X)
			end

			local maxX = currentItemManager.GridSize.X - rotatedSize.X
			local maxY = currentItemManager.GridSize.Y - rotatedSize.Y

			gridPos = Vector2.new(
				math.clamp(gridPos.X, 0, maxX),
				math.clamp(gridPos.Y, 0, maxY)
			)

			local isColliding = currentItemManager:IsColliding(self, { self }, gridPos, self.PotentialRotation)
			
			if isColliding == false then
				-- Get new ItemManager, is nil if no new ItemManager is found
				local newItemManager = nil
				if self.HoveringItemManager and self.HoveringItemManager ~= self.ItemManager then
					newItemManager = self.HoveringItemManager
				end

				-- Check for middleware and if it allows item move
				local middlewareReturn = nil
				if self.MoveMiddleware then
					middlewareReturn = self.MoveMiddleware(self, gridPos, self.PotentialRotation, self.ItemManager, newItemManager)
				end

				if middlewareReturn == true or middlewareReturn == nil then
					-- Move item
					self.Position = gridPos
					self.PositionChanged:Fire(gridPos)
					self.Rotation = self.PotentialRotation

					-- Switch ItemManager if the item was hovering above one
					if newItemManager then
						self:SetItemManager(self.HoveringItemManager)
					end
				end
			end

			self.PotentialRotation = self.Rotation

			self.HoveringItemManager = nil
			self.HoveringItemManagerChanged:Fire(self.HoveringItemManager)

			-- Update item positioning to current itemManager
			self:_updateItemToItemManagerDimentions(true, true, true, true)

			TweenService:Create(self.ItemElement, TweenInfo.new(0.1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {GroupTransparency = 0}):Play()
			self.ItemElement.ZIndex -= 1

			self._draggingTrove:Clean()

			self.IsDraggable = true
		end
	end))

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

Just use UIDragDetectors and you can use modulus (%) operator or floor division (//) to snap object to a grid.

PLZ FOR GOD SAKE DONT USE BLOATWARE LIKE “TROVE”, “JANITOR” OR “MAID” :skull: :pray:

1 Like