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

Data store not loading/saving?

Asked by 4 years ago
local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
--  if game.Workspace.DistributedGameTime > 3 then
--  repeat wait(1) until game.Workspace.DistributedGameTime > 3
--  end

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        myDataStore:GetAsync(player.UserId.."-stage")
    end)

    if success then
        stage.Value = data
    else
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
local StageValue = player.leaderstats.Stage.Value
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-stage", player.leaderstats.Stage.Value)
    end)

    if success then
        print("Data saved")
    else
        warn(errormessage)
    end
end)

Here is my script, I followed a tutorial online and got this. It doesn't work. Before you say this is a copy of my previous post, no one will answer it since there is already an answer (that doesn't work.) Now before you post an answer please check if it works because I don't want this to happen again. No errors, Data saved was printed, but I don't get the stat number back when joining, am I doing something wrong? The tutorial I watched was this. I've also looked in other places for data stores, none worked. Am I doing something wrong?

1
On line 19, you define data, but you fail to give it a value on line 21. Shouldn't line 21 be something like data = myDataStore:GetAsync(player.UserId.."-stage")? User#26971 0 — 4y
1
Side note: You don't have to define a new function when using pcall. Here's an example of how to avoid that in your case: `local success, data = pcall(myDataStore.GetAsync, myDataStore, player.UserId.."-stage")` User#26971 0 — 4y
1
If the pcall errors, `data` would be the error message. If pcall does not error, then `data` would be the return value of the GetAsync method. User#26971 0 — 4y
0
Thank you! Jack_Hase 85 — 4y

2 answers

Log in to vote
1
Answered by
SmartNode 383 Moderation Voter
4 years ago
Edited 4 years ago

You aren't setting the data.

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
--  if game.Workspace.DistributedGameTime > 3 then
--  repeat wait(1) until game.Workspace.DistributedGameTime > 3
--  end

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local stage = Instance.new("IntValue")
    stage.Name = "Stage"
    stage.Parent = leaderstats

    local success, returnval = pcall(function()
       return myDataStore:GetAsync(player.UserId.."-stage")
    end)

    if success then
        stage.Value = returnval
    else
        warn(returnval)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-stage", player.leaderstats.Stage.Value)
    end)

    if success then
        print("Data saved")
    else
        warn(errormessage)
    end
end)
0
thank you! Jack_Hase 85 — 4y
0
No problem, happy to help! SmartNode 383 — 4y
Ad
Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

You might've forgotten to enable API services for your game. If so you can just go to Configure game > Enable API services.

Otherwise i don't know what could be the problem, your script seems fine.

0
That is on, it was a previous problem, thank you for trying Jack_Hase 85 — 4y
0
He would've got some kind of error/warning if that were the case. Maybe you should've read his question/code a little more carefully before posting such a trite answer. User#26971 0 — 4y

Answer this question