How to make something move over if an item is in a slot

Im making a neo pet type game that is fully GUI, and I want to be able to have bowties, fedoras, etc to put on your pets. I have already made the purchasing feature. Heres the problem: I have an inventory and the hats just stack on each other, making it impossible to see whats on them, so I want to make the hat move over if it detects another hat is on a inventory slot.

I hope I’m not being confusing.

i don’t know how your inventory works but it’s quite simple. Just delete old hat when the new one is equipped. So for example if your inventory is a folder, then by using :ChildAdded(child) delete everything inside of a folder but child. Please explain your inventory system a bit if you still don’t know how to make it and i’ll try to tell you

Mkay, you purchase the hat from the shop. It goes into a inventory place.
Also. Im trying to make multiple slots, hence why I said “make something move over if an item is in a slot” not "Destroy hat if hat wants to replace it "

You just make a couple of folders, for example: inventory folder, cosmetic slot1 folder, cosmetic slot2 folder, cosmetic slot3 folder. Once you purchase something it goes to inventory folder and then player can redistribute items in a menu or whatever, if he equips something to slot1 then just move an item from inventory folder to slot1 folder, if he place something else in slot1 folder just return old item into inventory and move new item to slot1. This way you can make players have multiple items. That’s a simple inventory system

That doesn’t seem that efficient, but I’ll try it.

well, it shouldn’t be exactly folders, i just said folders because easy to explain, you can make string values for each slot or whatever you like, so like if slot1.Value == “TopHat” then equip a top hat accessory

Oh, I think your confused.
Im not making the inventory system, I already finished that.
Its just that hats stack on each other in the slots. So I want to make the hat move to another slot if it sees a hat on the slot it wanted to go.

before equiping just check if the slot if free, if not then make it go to another slot if it’s free. Again example with folders

if Slot1Folder:FindFirstChild("Accessory") then
   if Slot2Folder:FindFirstChild("Acessory") then
      --continue for more slots
   else
   newAccesory.Parent = Slot2Folder
   end
else
   newAccessory.Parent = Slot1Folder
end

I’ll test it, but theres gotta be a less hacky way.

i don’t know why would you call it “hacky” it’s a basic thing for me. You just check if the slot is free, if not then check if another slot is free, and continue till you find a free slot. ofc it’s not the entire script but i just said the way how you deal with your problem

This is the definition of “hacky”

if code = "1234" then return end
if code = "1356" then return end
if code = "1357" then return end
if code = "1234" then return end
if code = "1356" then return end
if code = "1357" then return end
if code = "1" then print("WIN!")

like you did is definitely bad… you don’t need to check for EACH accessory, just check if slot is free or full, again example with folders

local SlotFull = false
local Slot1Children = character.Slot1Folder:GetChildren()
for i, v in pairs(Slot1Children) do
   SlotFull = true
end
if SlotFull == true then
--move on to second slot
end

not the best script but i hope you get what i mean

I wrote a player system for an RPG game a while ago. What I did was simply save the player’s equipped items in a dictionary. So, by doing that I can easily check if a slot was filled or not.


local PData = {
Hat = <Instance>;
}

if PData[ItemType] then
PData[ItemType]:Destroy()
PData[ItemType] = <Instance>
else
PData[ItemType] = <Instance>
end

I actually wrote a custom enumerate system to keep track of the item types, you can do this and modulate all of the items so each type has a special ID so you can just simply reference it like this. This will allow you to add things much easier than your current method.

I don’t understand this code, at all. Can you explain it?

Use a UIGridLayout.

UIGridLayout (roblox.com)

Lets say you had an item.

local Item = {
Name = "Sword";
Type = "Primary";
Object = game.ServerStorage.Tools.Sword;
}

Now that we have an item we can equip it to the primary slot.

local Equipped = {}; -- We have nothing equipped atm.
function GetItem(ItemData)
Equipped[ItemData.Type] = ItemData.Object:Clone()
Equipped[ItemData.Type].Parent = game.workspace
end

function EquipItem(Name)
local ItemData = require(PathToModules[Name])
if ItemData and Equipped then
if Equipped[ItemData.Type] then
Equipped[ItemData.Type]:Destroy()
GetItem(ItemData)
else
GetItem(ItemData)
end
end
end

This still confuses me.
I dont know where to put this script, how to add things to it.
or pretty much anything

That code is an example of what to do. Its not supposed to be taken literally.

Oh, alright. I’ll try to make my own version!

Yeah, its the theory behind the system you need to work with. I also edited one of my replies. Take a look at that, it explains it more.