I think you just don't understand DataStores on a fundamental level.
Think of a DataStore as a dictionary or table.
1 | local dictionary = { [ "Player1" ] = 50 , [ "Player2" ] = 100 } |
This is a dictionary. The ["Player1"]
part is called the key. It's how you access its value, which is 50
.
When we type this:
1 | dictionary [ "Player1" ] = 50 |
We're setting the key Player1
to be equal to 50
.
And whenever we type this:
1 | print (dictionary [ "Player1" ] ) |
It gets the value assigned to the key Player1
and prints it out, which it'd print 50
.
DataStores work similarly.
With a DataStore, you can get the value of a key, and set the value of it, through the functions GetAsync
and SetAsync
, respectively.
GetAsync
can be used by doing the following:
1 | local value = dataStore:GetAsync(key) |
When we call the function GetAsync
on a DataStore, and pass in a key, it retrieves the value that's assigned to that key and returns it.
SetAsync
does the opposite:
1 | dataStore:SetAsync(key, value) |
It sets the value of the key to the value you pass in. You can retrieve that value whenever the player joins the game again, and set it when they're about to leave. Essentially, it's a global dictionary.
So, to fix your code, let's go over what you did wrong.
1 | game.Players.PlayerAdded:Connect( function (Player) |
4 | local PlayerKey = "Player_" .. Player.UserId |
6 | EquippedTool:GetAsync( { Player.Items.EquippedTool.Value } , Player.UserId) |
7 | EquippedBackpack:GetAsync( { Player.Items.EquippedBackpack.Value } , Player.UserId) |
Here, you're setting up the PlayerKey
variable and then you're not even using it. On top of that, you're passing in two arguments to the GetAsync function, when it only takes one, the key. On top of that, you're not doing anything with what the GetAsync function returns.
That bit of code should look something like this:
1 | game.Players.PlayerAdded:Connect( function (Player) |
2 | local PlayerKey = "Player_" .. Player.UserId |
4 | local equippedTools = EquippedTool:GetAsync(PlayerKey) |
5 | local equippedBackpacks = EquippedBackpack:GetAsync(PlayerKey) |
Now, you're getting the values stored in the key that's unique to the player, and you're storing those values in variables, for later use.
1 | game.Players.PlayerRemoving:Connect( function (Player) |
2 | local PlayerKey = "Player_" .. Player.UserId |
4 | EquippedTool:SetAsync( { Player.Items.EquippedTool.Value } , Player.UserId) |
5 | EquippedBackpack:SetAsync( { Player.Items.EquippedBackpack.Value } , Player.UserId) |
Over here, you're not using the PlayerKey
variable, and you're passing in the arguments in the wrong order.
If you take a look at this link, you'll see that you need to pass in the key first, and then the value you want to set the key to second.
So your code should look like this:
1 | game.Players.PlayerRemoving:Connect( function (Player) |
2 | local PlayerKey = "Player_" .. Player.UserId |
4 | EquippedTool:SetAsync(PlayerKey, { Player.Items.EquippedTool.Value } ) |
5 | EquippedBackpack:SetAsync(PlayerKey, { Player.Items.EquippedBackpack.Value } ) |
Here's the full corrected script:
02 | local DataStore = game:GetService( "DataStoreService" ) |
04 | local EquippedTool = DataStore:GetDataStore( "EquippedTool" ) |
05 | local EquippedBackpack = DataStore:GetDataStore( "EquippedBackpack" ) |
08 | game.Players.PlayerAdded:Connect( function (Player) |
09 | local PlayerKey = "Player_" .. Player.UserId |
11 | local equippedTools = EquippedTool:GetAsync(PlayerKey) |
12 | local equippedBackpacks = EquippedBackpack:GetAsync(PlayerKey) |
17 | game.Players.PlayerRemoving:Connect( function (Player) |
18 | local PlayerKey = "Player_" .. Player.UserId |
20 | EquippedTool:SetAsync(PlayerKey, { Player.Items.EquippedTool.Value } ) |
21 | EquippedBackpack:SetAsync(PlayerKey, { Player.Items.EquippedBackpack.Value } ) |
Sorry for the lengthy response, it's just that I wanted to ensure you understand exactly what you did wrong and how DataStores work.