How do I make hat add to my inventory when I buy it?

Hello ! I’m trying to make hat inventory and my problem is how can I make every seconds that if I buy the hat it gonna add the hat in my accessory inventory. I only can make the tool inventory. so this is my first I created hat/Accessory inventory and I need help. I literally have no Idea how to make it.

any help would be appreciated !

Thats all, Thanks :slight_smile:

1 Like

Well, this is how I would do this kind of stuff:

Once the player confirms the purchase in the shop, send a remote event, to check if the player has enough cash and as an argument send the hat’s name that the player wants to buy.

After that the server script should have a table that will hold all every player’s hats.

local playerHats = {
   ["random player"] = {
     "myCustomHat", "Black Fedora"
   }
}

If the player has enough cash to purchase the item, check if the player already owns that item to make sure you won’t duplicate it.
If the player doesn’t own it, then add the hat’s name to the table, like so:

myRemote.OnServerEvent:Connect(function(playerName, hatName)
    table.insert(playerHats[playerName.Name], #playerHats[playerName.Name])+1, hatName)
end)

After that, with the remote, send back the player’s new table that looks like so:

myRemote:FireClient(playerName, playerHats[playerName])

In the local script, listen to that event, to receive the new table.
After that, loop trough it, and update the inventory.

Disclaimer: Not sure if I said every necessary steps.

thank you very much for your reply ! I appreciate it.

1 Like

im sry to ask this if I disturb you because I didnt realize this. what does

[player name] meaning ?

Let me explain.
When you fire a remote event from a client, and when a server receives it, the first argument will be a player instance who called the remote event

but did I have to change it or just let it like that ? sry I am new at scripting

you can name it whatever you want it to be.
However, I forgot to say in my initial post, that you should modify this piece of code, since I forgot that the first argument isn’t a string, and that I forgot to replace [player name] to [playerName.Name]

myRemote.OnServerEvent:Connect(function(playerName, hatName)
    table.insert(playerHats[playerName.Name], #playerHats[playerName.Name])+1, hatName)
end)

And I really recommend you the Developer hub, because it explains pretty much every single roblox services, properties, etc…

1 Like

Okay got it Thanks for all your help! sry if I was disturb you.

1 Like

oof I got an error

image_2021-12-05_000906

or I do something wrong at here ?

Im gonna read your comment tomorrow

have a great day

sry if i was annoying to you

try removing the ) before the +1, it was probably a spelling error I made, since I didn’t write the code in studio.

by the way It have orange underline did I have to change it or ignore it on script that you correct me? and one more orange underline is in this code myRemote:FireClient(playerName, playerHats[playerName]) It have orange underline at (playerName, playerHats[playerName] and why is it FireClient ? didnt it have to be FireServer ? because on the script you type .OnServerEvent ? when I read the developer hub FireClient is for .OnClientEvent Please correct me if I’m wrong

I read on this two links or maybe I misunderstood. Because Im new ?

Sorry for bad grammar