Swapping Inventory Items around not working

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 to swap the items of my inventory for my game, but some strange things are happening described in the video
  2. What is the issue? Include screenshots / videos if possible!
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Inventory Script:
--[[
inventory

InventoryFrame Frame
	(script)
	ItemHolderFrame Frame
		ItemHolderTemplate TextButton
		ItemTemplate ImageButton
			DescriptionFrame Frame
				NameLabel TextLabel
				DescriptionLabel TextLabel

make a text label inside itemtemplate called QuantityLabel

remote function for swapping slots

equipping stuff event later i guess
]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")

local ItemModule = require(ReplicatedStorage.Modules.ItemModule)
local GetData = ReplicatedStorage.Remotes.GetData
local GetDataActive = ReplicatedStorage.Remotes.GetDataActive
local SwapSlots = ReplicatedStorage.Remotes.SwapSlots

local inventoryFrame = script.Parent -- put script inside of the thing
local itemHolderFrame = inventoryFrame.ItemHolderFrame
local itemHolderTemplate = inventoryFrame.ItemHolderTemplate
local itemTemplate = inventoryFrame.ItemTemplate

repeat wait() until GetDataActive:InvokeServer() -- bad code fix later
local inventory = GetData:InvokeServer("Inventory")
local settings = GetData:InvokeServer("Settings")

local selectedSlot

local function swapSlots(slot1, slot2)
	--local success = SwapSlots:InvokeServer(slot1, slot2) don't pay attention to this, i'm trying 
	local startSlot = itemHolderFrame[tostring(slot1)]
	local endSlot = itemHolderFrame[tostring(slot2)]
	
	if startSlot.Item ~= nil then
		if endSlot.Item ~= nil then
			print("1")
			startSlot.Item.Parent = endSlot
			endSlot.Item.Parent = startSlot
		elseif endSlot.Item == nil then
			print("2")
			startSlot.Item.Parent = endSlot
		end
	elseif startSlot.Item == nil then
		if endSlot.Item ~= nil then
			print("3")
			endSlot.Item.Parent = startSlot
		elseif endSlot == nil then
			print("4")
			selectedSlot = nil
			return nil
		end
	end
	selectedSlot = nil

	startSlot.BorderSizePixel = 1
	endSlot.BorderSizePixel = 1
end

for i = 1, inventory.Size do -- inventory size
	local template = itemHolderTemplate:Clone()
	template.Parent = itemHolderFrame
	template.Name = tostring(i)
	template.Visible = true
	template.MouseEnter:Connect(function()
		--fancy tween highlight
	end)
	template.MouseLeave:Connect(function()
		--fancy tween highlight end
	end)
	template.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			if selectedSlot then
				if selectedSlot == i then -- same selection, so unselect
					selectedSlot = nil
					template.BorderSizePixel = 1
				else -- second selection, so swap slots
					swapSlots(selectedSlot, i)
				end
			else -- select new
				selectedSlot = i
				template.BorderSizePixel = 5
			end
		end
	end)
end

for i,v in pairs(inventory.MainSlots) do
	local item = ItemModule[v.Id]
    local template = itemTemplate:Clone()
    template.Parent = itemHolderFrame[tostring(i)]
	template.Name = "Item"
	template.Image = item.Image
	template.Visible = true
	
    local descFrame = template.DescriptionFrame
	descFrame.NameLabel.Text = item.Name
	descFrame.DescriptionLabel.Text = item.Description
	
	template.MouseEnter:Connect(function()
		descFrame.Visible = true
	end)
	template.MouseLeave:Connect(function()
		descFrame.Visible = false
	end)
	template.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			if selectedSlot then
				if selectedSlot == i then -- same selection, so unselect
					selectedSlot = nil
					template.Parent.BorderSizePixel = 1
				else -- second selection, so swap slots
					swapSlots(selectedSlot, i)
				end
			else -- select new
				selectedSlot = i
				template.Parent.BorderSizePixel = 5
			end
		end
	end)
end

Here’s the image of my inventory gui
HELP1

Thanks for reading! Any help appreciated!

1 Like

I think it’s because of this right here:

startSlot.Item.Parent = endSlot
endSlot.Item.Parent = startSlot

You’re telling startSlot to become endSlot while telling endSlot to become startSlot directly after.
You’re basically telling endSlot to become endSlot again.
You could add a temporary variable to remedy this.

local middleSlot = startSlot

startSlot = endSlot
endSlot = middleSlot
middleSlot = nil
1 Like

I’m not at home right now, I’ll mark you as solution when I test it :grinning_face_with_smiling_eyes:

1 Like

Oh no it didn’t work! It’s the same as before :pensive:

Looking at the provided video. Does the first swap work?

No worries, I have figured it out! The problem was that when you swapped item 1 with item 2 for example, item 2’s slot would remain as item 2 in the slot of item 1

(i’m elated tysm for the motivation to come back to this)

Ah! Yes, this was my first answer to your problem. You were changing slot 1 to slot 2 and slot 2 to slot 1, but this would make slot 2 change to itself thus resulting in weird behavior.

But I’m glad I could instill some motivation!