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

Data Store one value saving, other not.

Asked by
TomsGames 225 Moderation Voter
10 years ago

NOTE: This is activated by the PlayerRemoving event.

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()

        ptnow = pts.Value
        hsnow = hs.Value                
        local asynccode = p.Name .. "pts"
        local asynccode2 = p.Name .. "hs"
            if not (ds:GetAsync(asynccode) or false) then
                ds:SetAsync(asynccode, 0) 
            end
            if not (ds:GetAsync(asynccode2) or false) then
                ds:SetAsync(asynccode2, 0) 
            end
        ds:SetAsync(asynccode, ptnow)
        ds:SetAsync(asynccode2, hsnow)

Sometimes when I play online and leave the value of "points" gets saved, other times it does not. It's really weird, is it because there is an inefficieny in my script?

Also it doesn't save "hsnow" (highscore). Here is the script which loads these values, maybe this has the issue:

NOTE: This is activated by the player joining.

        local asynccode = p.Name .. "pts"
        local asynccode2 = p.Name .. "hs"
        if not (ds:GetAsync(asynccode) or false) then
            ds:SetAsync(asynccode, 0) 
        end
        if not (ds:GetAsync(asynccode2) or false) then
            ds:SetAsync(asynccode2, 0) 
        end 

    local stats = Instance.new("NumberValue", p)
    stats.Name = "leaderstats"

    local HighScore = Instance.new("NumberValue", stats)
    HighScore.Name = "HighScore"
    HighScore.Value = (ds:GetAsync(asynccode2))

    print(ds:GetAsync(asynccode2))

    local Score = Instance.new("NumberValue", stats)
    Score.Name = "Points"
    Score.Value = ds:GetAsync(asynccode)

PLEASE NOTE: These are snippits from my scripts. The rest is just checks and connecting the function. Also it saves both values in the server test > start player system. Weird, huh?

2 answers

Log in to vote
2
Answered by 10 years ago
local ds = game:GetService("DataStoreService"):GetDataStore("PointStore")

        ptnow = pts.Value
        hsnow = hs.Value                
        local asynccode = p.Name .. "pts"
        local asynccode2 = p.Name .. "hs"
        ds:SetAsync(asynccode, ptnow)
        ds:SetAsync(asynccode2, hsnow)
    local ds = game:GetService("DataStoreService"):GetDataStore("PointStore")
    local asynccode = p.Name .. "pts"
    local asynccode2 = p.Name .. "hs"

local stats = Instance.new("NumberValue", p)
stats.Name = "leaderstats"

local HighScore = Instance.new("NumberValue", stats)
HighScore.Name = "HighScore"
HighScore.Value = ds:GetAsync(asynccode2) or 0
local Score = Instance.new("NumberValue", stats)
Score.Name = "Points"
Score.Value = ds:GetAsync(asynccode) or 0

Setting the value to 0 on the data store as a means of making sure the player has at least 0 points is the most resource-expensive way to do it. I also changed this so that it's working with it's own individual store, not the global one.

Ad
Log in to vote
0
Answered by 10 years ago

Hi,

 if not (ds:GetAsync(asynccode) or false) then

I don't know whether this solves your problem entirely, but 'or false' will not be included in an or statement. Infact :GetAsync() returns nil if it is found, so you only need the following;

 if not ds:GetAsync(asynccode) then

I hope that this helps you solve your issue.

Answer this question