Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I properly clone and save a players inventory in a GUI?

Asked by 4 years ago
Edited 4 years ago

I'm creating a custom inventory, called cauldron, and in the cauldron I have potions. The number of different potions a player has depends on their level, called Power. If they are Power 5, they have access to potions 1-5. I have this scrolling frame, and am trying to make it so that when the player opens the cauldron GUI, they see all of their potions there. So far I have a script that loops through based on the players Power.

I'm having an issue where I don't know how to make it so this GUI is only updated when needed. So far it will keep cloning the frames every time I click to open the GUI. Should I make this happen when the player first connects to the server, and then only update it when the reach a new Power level?

Also, should I be using a remote for this? I know I will have a remote for enabling the actual potion to be used by the player.

--When player clicks button, actual cauldron inventory pops up (CauldronScrollFrame)

cauldronOuterFrame = script.Parent.Parent.Parent:FindFirstChild("CauldronOuterFrame")
cauldronSideBtn = script.Parent

--get indiviual ptn frame that will be cloned
local template = script.Parent.Parent.Parent:WaitForChild("CauldronOuterFrame"):WaitForChild("CauldronScrollFrame")
                    :WaitForChild("Spell1")
print (template.Name)

function onClick()
--duplicate ptnFrame for every ptn based on Power

--get player's Power lvl to determine what ptns will be visible on cauldronFrame
local plrPower = game.Players.LocalPlayer.leaderstats.Power.value

for i = 2, plrPower do --i = 2 because you start with lvl1ptn, which is included in the total Power
    local new = template:Clone()
    new.Name = "Spell"..i
    new.Parent = template.Parent
end

--print "CaulSideBtn clicked, opening scrollFrame gui..."
if cauldronOuterFrame.Visible == false then
    cauldronOuterFrame.Visible = true
    --print "Frame made visible"
elseif cauldronOuterFrame.Visible == true then
    cauldronOuterFrame.Visible = false
    --print "Frame made invisible"
end
end

cauldronSideBtn.MouseButton1Click:Connect(onClick)

https://gyazo.com/8b74fb619ea3a7487238f2831048d657

1 answer

Log in to vote
0
Answered by
Nowaha 459 Moderation Voter
4 years ago
Edited 4 years ago

You can save their inventory contents whenever they leave by using a DataStore. When they join back, you can then load that data and put the items back into their inventory.

Refer to this, https://developer.roblox.com/en-us/articles/Data-store, and YouTube for further help. There's plenty of good tutorials around.

You can save all the data by doing something like this:

local playerInventory = {};

for index, object in pairs(Inventory parent of all the potions) do
    table.insert(playerInventory, object.Name);
    -- If you need an image, for example, you can do
    table.insert(playerInventory, {
        name = object.Name,
        image = object.ImageLabel.Image,
        power = 1}
    );
end;

inventoryDataStore:SetAsync(player.UserId .. "-inventory", playerInventory);

It loops through all the items inside of the player's inventory, and then adds them to a variable. Then at the end it pushes it to the data store.

Then when they log back in you can load the data and add inventory items again.

Ad

Answer this question