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

[Data Persistance] Saving Points Data, Wont work?

Asked by 10 years ago

Im trying to save leaderstats please help!

01pointskey = "Points"
02game.Players.PlayerAdded:connect(function(p)
03    leaderstats = Instance.new("IntValue", p )
04    points = Instance.new("IntValue", leaderstats)
05    leaderstats.Name = "leaderstats"
06    points.Name = "Points"
07    p:WaitForDataReady()
08    p:LoadNumber(pointskey)
09    points.Value = 1
10end)
11game.Players.PlayerRemoving:connect(function(p)
12    if p.leaderstats.Points.Value ~= 0  then
13    p:SaveNumber(pointskey, p.leaderstats.Points.Value)
14    end
15    end)

1 answer

Log in to vote
3
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

Data Persistance is deprecated, use DataStores

With Data Persistance, there isn't a garuntee that your data will be saved. So i'm gonna walk you through how to use DataStores.

With data Stores, you're going to have to know 4 methods. GetDataStore - Method of DataStoreService, use it to define the datastore you're using. GetAsync --Use it to get the data from a key, will return saved data or nil. SetAsync --Use it to save data to a given key. Update Async --Use it to update data from a key, second argument is a function that has a parameter of the old data and returns the new data.

So, we want to set up our DataStore.. we use DataStoreService

1local ds = game:GetService('DataStoreService'):GetDataStore("Points")

Next we make a PlayerAdded Event to load any data that could be there, if there is no data then set it to 0

01game.Players.PlayerAdded:connect(function(plr)
02    local key = tostring(plr.userId).."_points"
03    local data = ds:GetAsync(key) --Get the potential data
04    data = data or 0 --if data is nil, set to 0
05    local l = Instance.new('IntValue',plr) --create leaderstats
06        l.Name = 'leaderstats'
07    local p = Instance.new('IntValue',l) --create points
08        p.Name = "Points"
09        p.Value = data --Set points to data
10end)

Now we have to make sure that data was stored at some point.. so we use a **PlayerRemoving Event* to save data into a key.

01game.Players.PlayerRemoving:connect(function(plr)
02    local key = tostring(plr.userId).."_points" --key to save
03    local stat = plr.leaderstats.Points --points stat
04 
05--If there is data already saved we want to use UpdateAsync, other wise we want to use SetAsync
06 
07    local data = ds:GetAsync(key)
08    if data ~= nil then
09        ds:UpdateAsync(key,function(old) return stat.Value end)
10    else
11        ds:SetAsync(key,stat.Value)
12    end
13end)

You can read more about DataStores here

You also might want to consider using game.OnClose to set a function that saves everyone's data. in case of instances where your gam may crash or with 1 player servers the server could close before the script saves your data.

So all together?

01local ds = game:GetService('DataStoreService'):GetDataStore("Points")
02 
03game.Players.PlayerAdded:connect(function(plr)
04    local key = tostring(plr.userId).."_points"
05    local data = ds:GetAsync(key)
06    data = data or 0 --if data is nil, set to 0
07    local l = Instance.new('IntValue',plr)
08        l.Name = 'leaderstats'
09    local p = Instance.new('IntValue',l)
10        p.Name = "Points"
11        p.Value = data
12end)
13 
14game.Players.PlayerRemoving:connect(function(plr)
15    local key = tostring(plr.userId).."_points" --key to save
View all 23 lines...
Ad

Answer this question