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

Data stores don't work even after multiple attempts of making a working one?

Asked by 4 years ago
Edited 4 years ago

Hello all,

This is my first time working with data stores but for some reason every thing I've tried hasn't worked.

I've watched at least 3 tutorials, following closely each time and every time the data store fails to work/load data in.

On Studio I have API Services enabled, so I don't believe that's the problem.

Here is the Script , located in ServerScriptService

local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints")
game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = plr
    local cash = Instance.new("IntValue",folder)
    cash.Name = "Panther Points"
    local saveditems = Data:GetAsync(plr.UserId)
    if saveditems then
        cash.Value = saveditems:WaitForChild("Panther Points").value
    else
        cash.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
    Data:SetAsync(plr.UserId,saved)
end)

Any help would be greatly appreciated.

0
Oh let me just give you this link to a video bangdi124 -5 — 4y
0
I've tried multiple videos, but thanks for the help killerninja81 30 — 4y
0
See this one bangdi124 -5 — 4y
View all comments (4 more)
0
It didn't work. killerninja81 30 — 4y
0
@killerninja81 Try changing line 10 to, cash.Value = saveditems["Panther Points"] and let me know if it works or not. xInfinityBear 1777 — 4y
0
ok killerninja81 30 — 4y
0
nope, didnt work killerninja81 30 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hey, I know a lot of people have this problem where there's nothing wrong with their script, but the server fails to save their data and reload it when they rejoin. This happens when the server shuts down before the last player leaves, thus not running your PlayerRemoving function which is supposed to save your data. This could also happen when you're playing solo in the Studio. To fix this problem, use :BindToClose. :BindToClose will allow all your remaining functions to run before the server shuts down. Do not delete anything from your original script. Simply copy and paste your PlayerRemoving function (take out PlayerRemoving though and use a loop to loop through all the players in the server) into the :BindToClose function at the end.

local Data = game:GetService("DataStoreService"):GetDataStore("PantherPoints")
game.Players.PlayerAdded:Connect(function(plr)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = plr
    local cash = Instance.new("IntValue",folder)
    cash.Name = "Panther Points"
    local saveditems = Data:GetAsync(plr.UserId)
    if saveditems then
        cash.Value = saveditems:WaitForChild("Panther Points").Value -- capitalize the v in Value as well
    else
        cash.Value = 0
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
    Data:SetAsync(plr.UserId,saved)
end)

game:BindToClose(function()
    for _, plr in pairs(game.Players:GetPlayers()) do
         local saved = {["Panther Points"] = plr:WaitForChild("leaderstats"):WaitForChild("Panther Points").Value}
         Data:SetAsync(plr.UserId,saved)
    end
end)
0
Thanks a lot! It works now, just one little problem, it says that in line 10, it attempts to call a nil value killerninja81 30 — 4y
0
Oh, I didn't catch that. saveditems is your data. Just do "cash.Value = saveditems" AntiWorldliness 868 — 4y
0
Wait no, I fixed it, I made it so that it was like 'if saveditems ~= nil' and swapped the positions of the next 3 lines killerninja81 30 — 4y
Ad

Answer this question