Issues with moving items in inventory

The items just act extremely weird when I try to click on them. Each slot of the inventory has this same code:

local TheValleyFunctions = require(workspace.ModuleScripts.TheValleyFunctions)

local Player = game.Players.LocalPlayer
local ClickedInventoySlot = require(Player.PlayerGui.ScreenGUI.Inventory.ClickedInventorySlot)


local UIS = game:GetService("UserInputService")

local Hitbox = script.Parent
local SlotFrame = Hitbox.Parent
local ItemImage = Hitbox.Parent.ItemImage
local SlotImage = Player.PlayerGui.ScreenGUI.ClickedSlot

local Item
local HitBox = script.Parent
local Slot = HitBox.Parent
local Inventory = Slot.Parent.Parent

local Tags = Slot.Tags

local function PlaceInSlot()
	ItemImage.Image = SlotImage.Image
	ItemImage.Counter.Text = SlotImage.Counter.Text
	Slot.Count.Value = ClickedInventoySlot["Amount"]
	Slot.ItemName.Value = ClickedInventoySlot["Item"]
	Slot.Occupied.Value = true
	
	
end

local function ResetSelection()
	SlotImage.Image = ""
	SlotImage.Counter.Text = ""
end

local function Select(SlotInfo,Swap)
	local Inventory = TheValleyFunctions.GetInventory(Player)
	local SlotInformation
	if Swap == true then
		ClickedInventoySlot["Amount"] = Slot.Count.Value
		ClickedInventoySlot["Item"] = Slot.ItemName.Value
		
	else
		ClickedInventoySlot["Amount"] = SlotInfo["Num"]
		ClickedInventoySlot["Item"] = SlotInfo["Item"]

		for i,v in Inventory.Data do
			if tostring(i) == SlotFrame.Name then
				SlotInformation = Inventory.Data[i]
				for a,b in SlotInformation do
					ClickedInventoySlot["Item"] = a
					ClickedInventoySlot["Amount"] = b
					print(ClickedInventoySlot)
				end
			end
		end
	end
	
	
	print("Slot Information: ",SlotInformation)
	local Position = UIS:GetMouseLocation()
	local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
	
	if Swap == false then
		SlotImage.Image = ItemImage.Image
		SlotImage.Counter.Text = ItemImage.Counter.Text
		SlotImage.Position = ImagePosition
		ItemImage.Image = ""
		ItemImage.Counter.Text = ""

		TheValleyFunctions.DeleteSlot(Player,tonumber(Slot.Name))

		Slot.Occupied.Value = false
	else
		
	end
	
	
	UIS.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement then
			local Position = UIS:GetMouseLocation()
			local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
			SlotImage.Position = ImagePosition
			task.wait()
		end
	end)
	
end

script.Parent.MouseButton1Click:Connect(function()
	print("Clicked Slot")
	local SlotInfo = {
		["Item"] = Slot.ItemName.Value,
		["Num"] = Slot.Count.Value
	}
	if Slot.Occupied.Value == false then
		print("Slot is not occupied")
		if Inventory.ItemSelected.Value == true then
			print("Item already selected")
			TheValleyFunctions.ReplaceItem(Player,{
				Num1 = ClickedInventoySlot["Amount"],
				Item1 = ClickedInventoySlot["Item"],
				SlotBeingReplaced = Slot.Name
			})
			
			PlaceInSlot()
			ResetSelection()
			
			Inventory.ItemSelected.Value = false
		end
	else
		print("Slot occupied")
		local InventoryTable = TheValleyFunctions.GetInventory(Player)
		if InventoryTable.Data[tonumber(Slot.Name)][Slot.ItemName.Value] then
			print("Item confirmed")
			if Inventory.ItemSelected.Value == true then
				print("Item already selected")
				local NewSlotInfo = {
					["Item"] = Slot.ItemName.Value,
					["Num"] = Slot.Count.Value
				}
				TheValleyFunctions.ReplaceItem(Player,{
					Num1 = ClickedInventoySlot["Amount"],
					Item1 = ClickedInventoySlot["Item"],
					SlotBeingReplaced = Slot.Name
				})
				Select(NewSlotInfo,true)
				PlaceInSlot()
						
			else
				Inventory.ItemSelected.Value = true
				print("Item not selected")
				Select(SlotInfo,false)
				
			end	
		end
	end
end)

--script.Parent.MouseButton1Click:Connect(function(player)
	--Inventory.SlotClicked:Fire(Slot,Tags)
--end)

The module script stores the information of what was clicked:

local ClickedInventorySlot = {
	["Item"] = nil,
	["Amount"] = nil
	
}

return ClickedInventorySlot
1 Like

Dont intersect the backend and the frontend code, it never ends fine.
all you can do its refactor some steps.

  1. Click the item
  2. Remove the item from slot FOR CLIENT ONLY (Just in the UI)
  3. Click the new slot
  4. Swap the actuall item positions
1 Like

What server-side actions should I do?

1 Like

Its quite simple, the only thing you should do on server, just swap the slots when needed.

1 Like


I made this proof of concept for what needs to be done for each of the 3 scenarios.

Last step in pink box should be “Set module’s information to clicked item variable”

1 Like

You got it wrong, my idea was round these steps. (Ill elaborate each one)

  1. Click on the slot (Client)
    1.1 Remove Item Frame or make it transparent (Client)
  2. Click the second slot (Client)
    2.2 Swap the slots data (Server)
    2.3 Clear the cursor’s item (Client)

As you can see, there is minimum server actions required as it should be.
There is no need to remove the slot data on click or something like that. Remember that data is fragile so you should keep it safe from actions you dont need.

This covers scenario 1 (in step 1) and 3 (in step 2). Would what I put for scenario 2 still work? (Top right corner) I’m restructuring the whole code since it’s just a jumbled mess to navigate

Sorry, i probably got your problem wrong.
I still dont quite understand the logic behind it, but based on the info i got, it could probably work.
As for scenario where you need to take item A and click on slot with item B
This shoud look smth like that:

  1. Take Item A (COPY the slot information to the Cursor) and hide the original slot
  2. Place Item A in the same slot where Item B saved.
  3. Erase the Cursors Info and Copy the Item B data to Cursor.
  4. Erase the Old Data from Slot where Item B been and put new Item A data.
  5. Put Item B somewhere
    5.1. In case player closes inventory, put Item B in the any free slot
    5.2. In case player places Item B in the same slot with Item C repeat the steps from 1-4
1 Like