Help needed with datastore p.2

Hello!
I am making this post since my last one went dead without me getting an answer. I’m trying to save and load all the items in a game folder but I’m not doing it right and don’t know how to fic it.
Here’s what I’m trying to save:

image

Here’s what my code currently looks like, and yes it’s bad but I don’t know datastores well yet.

Yeah, it’s bad. Any help is welcome!

Thanks!

First of all, you didn’t create the path to the player variable.

Plus this needs to be in a player removing function.

1 Like

I would use localplayer right.

Also I have zero clue where everything goes

Place your code inside this:

game:GetService("Players").PlayerRemoving:Connect(function(player)

end)
1 Like

No keep your code inside a server script

1 Like

Also replace the last bit of your code with this:

if succ then
    warn("Saving done")
else
    warn("Saving failed"..err)
end
1 Like

Localplayer can only be called on a local script.

1 Like

How should I reference the player then?

PlayerAdded and PlayerRemoving returns the player object

Example:

game.Players.PlayerAdded:Connect(function(plr) 
     print(plr.Name) -- expected output: the player's username
end) 
1 Like

You should search youtube for reference or tutorials on datastore, the roblox creator documentation also helps too.

1 Like

I looked at the documentation. It helped on the playeradded and playerremoved but not really on the datastore, unless I’m looking at the wrong one.

So just to be clear, if my current variable is player, I’d change the function() name from plr to player

PlayerAdded and PlayerRemoving is not really necessary in the datastore, BUT it helps getting the player and checking whenever someone is leaving.

Heres how datastore works from my experience

:GetDataStore(stringhere)

This is like a key accessing the storage, you can have multiple storage “box” in one game. To access the “box” just put in a string. For example, a “box” for swords, just input :GetDataStore("Swords"). It can be whatever key you like. It creates a new “box” if it doesn’t exists.

:GetAsync(string/number) basically gets a data from the “box” you just called. The string/number can be whatever

If you want a single player’s data, use the player’s UserId, it will return nil if the player doesn’t have a data(new player)

If you want a SERVER data, aka global data, you can just input your custom string ex: :GetAsync("SwordName")

:SetAsync(string/number, table) Basically just saves the data, it’s like GetAsync, but it’s the saving function.

1 Like

Thank you so much for the explanation! If I am reading this right, get async would be used for loading, and set async would be used for saving?