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

Saving String in my DataStore wont work?

Asked by 3 years ago

Basically I was scrolling through google trying to find my answers, going through a lot of threads not finding my answer.

I am trying to save 2 String Values for my game so they can Auto Equip when they first join.

A brief DataStore Script I'm using is:

-- Locals:
local DataStore = game:GetService("DataStoreService")

local EquippedTool = DataStore:GetDataStore("EquippedTool")
local EquippedBackpack = DataStore:GetDataStore("EquippedBackpack")

-- Load Equipped Stuff:
game.Players.PlayerAdded:Connect(function(Player)
    wait()

    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:GetAsync({Player.Items.EquippedTool.Value}, Player.UserId)
    EquippedBackpack:GetAsync({Player.Items.EquippedBackpack.Value}, Player.UserId)
end)
--

-- Save Equipped Stuff:
game.Players.PlayerRemoving:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:SetAsync({Player.Items.EquippedTool.Value}, Player.UserId)
    EquippedBackpack:SetAsync({Player.Items.EquippedBackpack.Value}, Player.UserId)
end)
--

If anyone could answer this I would be grateful.

ALSO if you have any questions I am willing to answer them.

1 answer

Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
3 years ago

I think you just don't understand DataStores on a fundamental level.

Think of a DataStore as a dictionary or table.

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:

dictionary["Player1"] = 50

We're setting the key Player1 to be equal to 50.

And whenever we type this:

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:

local value = dataStore:GetAsync(key)
print(value)

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:

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.

game.Players.PlayerAdded:Connect(function(Player)
    wait()

    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:GetAsync({Player.Items.EquippedTool.Value}, Player.UserId)
    EquippedBackpack:GetAsync({Player.Items.EquippedBackpack.Value}, Player.UserId)
end)

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:

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    local equippedTools = EquippedTool:GetAsync(PlayerKey)
    local equippedBackpacks = EquippedBackpack:GetAsync(PlayerKey)
end)

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.

game.Players.PlayerRemoving:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:SetAsync({Player.Items.EquippedTool.Value}, Player.UserId)
    EquippedBackpack:SetAsync({Player.Items.EquippedBackpack.Value}, Player.UserId)
end)

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:

game.Players.PlayerRemoving:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:SetAsync(PlayerKey, {Player.Items.EquippedTool.Value})
    EquippedBackpack:SetAsync(PlayerKey, {Player.Items.EquippedBackpack.Value})
end)

Here's the full corrected script:

-- Locals:
local DataStore = game:GetService("DataStoreService")

local EquippedTool = DataStore:GetDataStore("EquippedTool")
local EquippedBackpack = DataStore:GetDataStore("EquippedBackpack")

-- Load Equipped Stuff:
game.Players.PlayerAdded:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    local equippedTools = EquippedTool:GetAsync(PlayerKey)
    local equippedBackpacks = EquippedBackpack:GetAsync(PlayerKey)
end)
--

-- Save Equipped Stuff:
game.Players.PlayerRemoving:Connect(function(Player)
    local PlayerKey = "Player_" .. Player.UserId

    EquippedTool:SetAsync(PlayerKey, {Player.Items.EquippedTool.Value})
    EquippedBackpack:SetAsync(PlayerKey, {Player.Items.EquippedBackpack.Value})
end)
--

Sorry for the lengthy response, it's just that I wanted to ensure you understand exactly what you did wrong and how DataStores work.

1
I just have a question about the Load Equipped Stuff. Why do you put local and then equippedTools on it? And how do I make it load? ALSO thank you SO much for explaining really useful. I will accept the answer once you just explain me one thing, but TYSM hzesbemad 24 — 3y
1
Never mind found out my problem THANK YOU SO MUCH for your time honestly man. hzesbemad 24 — 3y
0
Putting the word "local" before the name of a variable when defining it just helps the script to read it faster. Just an optimization. Rinpix 639 — 3y
0
And np :) Rinpix 639 — 3y
Ad

Answer this question