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

DataStore being generally confusing. Help, anyone?

Asked by 5 years ago
Edited 5 years ago

The expected result of the SaveData() function in the code below is to print "Saved data for Player1." However, it NEVER prints this. I'm having trouble because this also occurs when I replace UpdateAsync() with SetAsync(). No error is returned either.

Here's the whole script:

local DataManager = {}

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("PlayerData")

local SessionData = {} -- Current game data
local StartingData = -- Data for players who have never played.
{
    Cash = 0, 
    Experience = 0, 
    Level = 0, 
    TotalDamage = 0, 
    EquippedWeapon = "Stick", 
    Weapons = {"Stick"}
} 

local function SaveData(Player)

    local PlayerId = Player.UserId
    local Data = SessionData[PlayerId]

    print(Player.Name .. " left. Saving data.")

    if Data then 

        local success, err = pcall(function()
            PlayerData:UpdateAsync(PlayerId, function(old) return Data end)
        end)

        if not success then print(err) error("Cannot save data for " .. Player.Name .. "!") end

    end

    print("Saved data for " .. Player.Name .. ".")

end

game.Players.PlayerRemoving:Connect(function(Player)        
    SaveData(Player)
end)

return DataManager
0
I'm guessing this is not the only code, you also missed the first and last line in the codeblock. To begin if there are no errors it's either working or not running, try printing something on line 1 to see if it is. If it's not running it's either disabled or misplaced. gullet 471 — 5y
0
I've posted the whole script. I can't seem to find the problem, as it runs fine otherwise. negativize 0 — 5y
0
Why is it so spaced out >.< Shawnyg 4330 — 5y
0
How I code man, I don't like big clumps of text. Same reason they alternate colors on charts negativize 0 — 5y
View all comments (5 more)
0
Can you at least cut your code into sections and tell us what each of those sections do? There's a lot of things to read and saving some time means I could also be helping someone else. lazycoolboy500 597 — 5y
0
This depends are you testing this in studio or on a real server? GetGlobals 343 — 5y
0
Will cut code and using APS, so it shouldn't matter @Syntax negativize 0 — 5y
0
Cut the code. I left in the code that I have the problem with negativize 0 — 5y
0
Why use UpdateAsync when you're using it like you'd use SetAsync? SetAsync overrides the current data, which is what you're already doing Shawnyg 4330 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

There is a simpler way of doing it... Here: (Hope it helps!!)

local Service = game:GetService('DataStoreService'):GetDataStore('CHANGE_THIS')--Change whats inside the '' not the variable
game.Players.PlayerAdded:connect(function(plr)
    local stats = plr:WaitForChild('leaderstats')
    local Exp = stats:WaitForChild('Exp')--Change whats inside the '' not the variable
    local Level = stats:WaitForChild('Level')--Change whats inside the '' not the variable
    local uniquekey = 'id-'..plr.userId
    local GetSaved = Service:GetAsync(uniquekey)
    if GetSaved then
        Exp.Value = GetSaved[1]
        Level.Value = GetSaved[2]
    else
        local SavingNums = {Exp.Value,Level.Value}
        Service:SetAsync(uniquekey,SavingNums)
    end
end)
game.Players.PlayerRemoving:connect(function(plr)
    local stats = plr:WaitForChild('leaderstats')
    local Exp = stats:WaitForChild('Exp')--Change whats inside the '' not the variable
    local Level = stats:WaitForChild('Level')--Change whats inside the '' not the variable
    local uniquekey = 'id-'..plr.userId
    local Savetable = {Exp.Value,Level.Value}
    Service:SetAsync(uniquekey,Savetable)
end)--Hope this helps you out!

Enjoy.

0
If you want it to give weapons when you unlock them then just give me a shout. ChrisThrRoblox1234 12 — 5y
Ad

Answer this question