Inventory system save slots

I didn’t bother reading the full thread here because this is already solved but I thought I’d drop in another way of saving data to a DataStore for inventory systems…

You can use HttpService:JSONEncode() to encode a table into a string then save that to the data store.

So basically when the player buys a tool, it would get their encoded tool data from the datastore, decode it by using HttpService:JSONDecode() which converts it back into a useable table, then simply use table.insert() to insert the new name of the tool. Then you could JSONEncode the edited table again, then use DataStore:SetAsync() or DataStore:UpdateAsync() (which is more recommended in this case) to update the datastore with the newly bought information.

Now you can rinse and repeat the process for all other buy processes.

FYI You can use a for loop to loop through the Decoded table from the datastore to find the tool names in the server storage then give them to the players backpack when they spawn in.

Don’t forget to use local success, error = pcall(function()!

Saving a table is way easier, and his just normal scripter so we don’t confuse him and yes thats a complicated way doing of it

But I’ve told you don’t clone it from local script, it may/may not work sometimes, mostly dosen’t work and I sent the remote events step by step and you said you did that, you removed that remote events?

It’s actually not complicated at all! I just explained pretty poorly. Reading over it again, I can see how it’s confusing. All your really doing is grabbing 2 services and using 1 line of code to encode / decode.

It’s super simple when you do it enough times. JSONEncode / JSONDecode is a good thing to learn anyways. It’s useful for situations that aren’t like this one.

Like I said, it was simply another way of doing it (which I find to be more reliable because you don’t gotta edit scripts too much)

1 Like

If you’re trying to make it properly have a max inventory slot system, I recommend using an IntValue which is the main part of that UI. When you click on one of the buttons to equip it, tell the server to Add 1 onto the IntValue. When the player unequips, tell the server to subtract 1 from the IntValue to act as 1 slot being freed up.

By the looks of it, the max inventory size is 4, so make your the server has that information as well stored in a variable. It would compare the number on the players IntValue, and if it’s equal to 4 already, then it won’t let them equip the tool.

You just gotta make sure you incorporate the math so the server knows the limit.

I alr made one save system script for IntValues and works

1 Like

Oh okay! It just seems like the video you sent has an issue with it locking up the equip button because it thinks you reached max inventory space. That’s what I recommended above in my most recent post, but if it works just fine, then that’s good! Hope this helped for future projects anyways.

1 Like

Rn I am only having issues with unequip and equip for buttons (when the backpack got +3 tools and you cant equip more tools)

Yeah this is where I recommended using another IntValue for something like inventory space. Make it so each button that tells the server to give the tool to the player, to also add 1 onto that Inventory Space IntValue. Then you could also perform checks on the server to see if the IntValue is at the max number or not (ex: 4).

You could always use a RemoteFunction to return false if the Inventory Space is exceeded, which would make the label turn red saying “Max Inventory Space” or something.

I did other post before about max limit and yes the IntValue idea is really good but he told me for use something like

local maxnum = 3 --max number of items in backpack

script.Parent.MouseButton1Click:Connect(function()
     if #player.Backpack:GetChildren() < maxnum then
          tool:Clone().Parent = player.Backpack
      end
end)

yes I am trying to do that but I am having this problem.(when you hit the limit of tools and you unequip you cant equip other tool agian.)

It appears you’re not telling the server to subtract from the MaxLimit once you press the “Unequip” version. I looked over your past code and it didn’t say that either.

If your max limit is 3 or 4, in the earlier video you sent, the output just counted up in numbers per equip, but it never went down when you unequipped. You’re adding up to the max limit, but never subtracting from it once you hit the Unequip part of the button.

Therefore the server thinks that it’s maxed out, when it’s not

And if you’re using this method

local maxnum = 3 --max number of items in backpack

script.Parent.MouseButton1Click:Connect(function()
     if #player.Backpack:GetChildren() < maxnum then
          tool:Clone().Parent = player.Backpack
      end
end)

I don’t think it’s going to work that well, so try to incorporate IntValue instead.

I changed the script basicly bc first 2 buttons was with -1 +1 just for test but rn I cant unequip

so how I am gonna do that? I make one IntValue with Value 3?

I’m writing up a script right now, I will update this reply when I get it all typed out.

Sorry for taking so long…

I’m new to using return. If something doesn’t work, please ask someone else how to properly use return. The way I did it seemed the most logical with no access to studio atm.

local MaxLimit = 4 -- idk why I keep using 4, but that's our limit here. Pretend this is on the server. Anything on the client can be exploited.

RandomToolIMadeUp = game.ServerStorage.FunnyTool

local RemoteFunction = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunction") -- feel free to change this to the name of the RemoteFunction that you might be using for this.

-- Server Script --

RemoteFunction.OnServerInvoke:Connect(function(Player, IntValue)

    if IntValue >= MaxLimit then

        return false

    elseif InventorySpaceUsed < MaxLimit then

        RandomToolIMadeUp:Clone().Parent = Player.Backpack

        return true

    end

end)

-- LocalScript --

local LimitFromClient = 4

local IntValue = script:WaitForChild("NameOfIntValue").Value

script.Parent.MouseButton1Click:Connect(function()

    IntValue = IntValue + 1

    local FiredEvent = RemoteFunction:InvokeServer(IntValue)

    if FiredEvent == false then

        -- here is where you would put color changing and text changing and stuff.

    elseif FiredEvent == true then

        -- do whatever you want like changing color to green and saying equipped or such.

    end

end)

-- I'm not too familiar with return statements, so if something doesn't work, it might be the way I used the return statement. I recommend someone else goes over this code throughly.

“InventorySpaceUsed” is what? You only used in that line.

Oh sorry, my apologies. Please replace that with IntValue.

If this doesn’t end up working right, it has something to do with the way I used return. I’m still new to returning so please bare with me. It just looks something like that in a sense

Also I am having this problem too " OnServerInvoke is not a valid member of RemoteEvent “ReplicatedStorage.InvLimit”

Oh that’s because I was hinting at you to switch from RemoteEvent to RemoteFunction because a RemoteFunction allows you to return info to the client after the ServerInvoke has completed. This is useful for showing input for the user.

You could technically do it with RemoteEvent's too, it’s just a different process