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

Is there any way to save values to a player and load it when they return?

Asked by
hozann 75
7 years ago

I've been looking everywhere but i can't get a thorough explanation of how, if i could perhaps get an example of how to use the method too, i would be extremely grateful.

0
Have you looked at the Datastores page already? honeygold13843 136 — 7y
0
I have, but i don't understand most of it. hozann 75 — 7y
0
How well do you understand tables? honeygold13843 136 — 7y
0
Use data Persistance! Kyokamii 133 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Tables Just in case you don't understand tables, I'll go ahead and go over them. :)

Tables are a way you can store multiple values, such as numbers, strings, other tables, etc. They store things in such a way that you can draw them out.

So that if I have the following code below:

--Players already in game: TestingPlace1337, honeygold13843, doctortardiswho, Zurzur

local playerTable = {}

for i, plr in pairs(game.Players:GetChildren()) do
    table.insert(playerTable, plr.Name)
    wait()
end

for i, plr in pairs(playerTable) do
    print(i, "-", plr)
end

The following code will output something along the lines of

1 - doctortardiswho
2 - honeygold13843
3 - TestingPlace1337
4 - Zurzur

Table when used as a function/method allows you to do all sorts of things, one as you can see above 'table.insert(table, pos[option], value)'.

These are very helpful whether if be in the sense of locally saving values, or even helping you sort data to be stored.

DataStores

I'm going to do my best to explain DataStores, although anyone here could probably do a much better job. I also am gonna state, it took me a little bit to catch onto DataStores too. ;)

Anyways...

DataStores are a long list that store values on a paired system, that Roblox will save on their servers to help aid you in Data Storage. Much like a table, they have a key, and a value. However there are some restrictions that we face already.

Keys must be strings.
Values can't be Roblox types such as Vector3, Instance, or Part.

DataStores are mainly used to store things such as in game currency or even a players inventory. You can reference 'a' DataStore via the following:

local DataStore = game:GetService("DataStoreService"):GetDataStore(name)

Whereas the name is the name of your DataStore, so it can realistically be anything you want to name it.

Methods

:GetAsync(key)
:SetAsync(key, value)
:UpdateAsync(key, transformFunction())
:IncrementAsync(key, delta)
:OnUpdate(key, callbackFunction())

DataStore comes with quite a few built-in methods, of course it wouldn't be much use without them, right?

I'll go ahead and explain them to the best of my ability and how they can be used.

:GetAsync(key) This method requires a key, a string, to be passed through, so it can retrieve the value tied to that key. If there is no value associated with the presented key, it will return nil.

local DS = game:GetService('DataStoreService'):GetDataStore("TestStore1337")
local key = "thisisatestkey"

print(DS:GetAsync(key)) --Outputs nil

:SetAsync(key, value) This method requires a key much like :GetAsync does, but allows you to provide a value as well. This will save the value to the key on the DataStore.

local DS = game:GetService('DataStoreService'):GetDataStore("TestStore1337")
local key = "thisisatestkey"
local value = 1337

DS:SetAsync(key, value)
value = 0
print("Value:", value, "DataStoreValue:", DS:GetAsync(key)) --Output: Value: 0 DataStoreValue: 1337

:UpdateAsync(key, transformFunction()) This is a method I don't use often, but have used a time or two. It allows you to update a value using a function, so you could have a variable and based on that variable change the value associated with the key.

--This block provided by the wiki.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:connect(function(player)
    local key = "user_" .. player.userId
    --we want to give 50 points to users each time they visit
    DataStore:UpdateAsync(key, function(oldValue)
        local newValue = oldValue or 0 --oldValue might be nil
        newValue = newValue + 50
        return newValue
    end)
end)

:IncrementAsync(key, delta) This method will work as common sense should state, it increments the value of key based on delta, and returns the incremented value.

local DS = game:GetService('DataStoreService'):GetDataStore("TestStore1337")
local key = "thisisatestkey"
local value = 0

DS:SetAsync(key, value) --Let's set this to 0 for the time being, since we left it at 1337.

value = DS:IncrementAsync(key, 1337)
print(value) --This will output 1337 because the DataStore updated the value to 1337, by 1, and then returned the value and stored it in the variable value.

:OnUpdate(key, callbackFunction()) This method is used to call the callbackFunction whenever the key is updated.

--This block provided by the wiki

local DataStore = game:GetService("DataStoreService"):GetDataStore("Test")

local connection = DataStore:OnUpdate("key", function(value)
    print("the key was changed to " .. value)
end)

DataStore:SetAsync("key", 1)
DataStore:SetAsync("key", 2)

connection:disconnect()

DataStore:SetAsync("key", 3)

Output: the key was changed to 1 the key was changed to 2

This is the basics of DataStores, however there are still a lot of restrictions you have to keep in mind.

Request Limit

Gets            GetAsync                                    60 + numPlayers * 10
Sets            SetAsync, IncrementAsync, UpdateAsync   60 + numPlayers * 10
OnUpdate        OnUpdate                                    30 + numPlayers * 5

Maximum Number of Characters

Key     50
Name    50
Scope   50
Data    260000

I hope this helps you out. Any questions please feel free to comment. ;)

Ad

Answer this question