You can write your topic however you want, but you need to answer these questions:
-
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 -
What is the issue? Include screenshots / videos if possible!
-
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
Thanks for reading! Any help appreciated!