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

Why is the Data Store in my game not saving the Players Stats?

Asked by 6 years ago

Hello, I have been stuck with this glitch for ages. I've tried everything to save stats on my games, but there is no 100% working save, which upsets me as I have literally done everything I could and spent over a year trying to solve this kind of glitch. I have lately decided to convert to a Data Store since I got told that might work, however even the Data Store isn't working.. The idea of my game is for the Player to have 3 Stats which will save; Their Level, Experience and their Genetics. It wasn't saving at first, but I got told I need to activate API Access, which I have done but now the Stats don't save at all.. Could someone please either help me fix the script or make a new one involving the 3 stats mentioned above inside a Folder in the Player called "LevelData"? I would be very grateful and would be happy to pay you ROBUX for it after making sure it doesn't break or have flaws. I know this might be a big ask but I sometimes feel like ROBLOX deliberately singles out my games so that the data never saves, but will save perfectly on everyone else's, even if they lose connection. This is the Data Store I recently made:

local module = {}
local Storage = game:GetService('DataStoreService'):GetDataStore('StatHolder')

game.Players.PlayerAdded:connect(function(player)
    local le = Instance.new("Folder")
    le.Name = "LevelData"
    le.Parent = player
    local Exp = Instance.new("NumberValue")
    Exp.Name = "Exp"
    Exp.Parent = le
    Exp.Value = 0
    local SavedExp = Storage:GetAsync(player.userId.."-Exp")
    if SavedExp ~= nil then
        Exp.Value = SavedExp
    end
    local Level = Instance.new("NumberValue")
    Level.Name = "Level"
    Level.Parent = le
    Level.Value = 0
    local SavedLevel = Storage:GetAsync(player.userId.."-Level")
    if SavedExp ~= nil then
        Level.Value = SavedLevel
    end
    local Genetics = Instance.new("NumberValue")
    Genetics.Name = "Genetics"
    Genetics.Parent = le
    Genetics.Value = math.random(1,100)
    local SavedGenetics = Storage:GetAsync(player.userId.."-Genetics")
    if SavedGenetics ~= nil then
        Genetics.Value = SavedGenetics
    end
    while true do
        wait(0.1)
        if Exp.Value > 10000000 then
            Exp.Value = 10000000
        end
        Exp.Changed:connect(function()
            Level.Value = math.floor(Exp.Value / 1000)
        end)
end
end)

game.Players.PlayerRemoving:connect(function(player)
    local id = player.userId
    local ExpValue = player.LevelData.Exp.Value
    Storage:SetAsync(id.. "-Exp", ExpValue)
    local LevelValue = player.LevelData.Level.Value
    Storage:SetAsync(id.. "-Level", LevelValue)
    local GeneticsValue = player.LevelData.Genetics.Value
    Storage:SetAsync(id.. "-Genetics", GeneticsValue)
end)
return module

This is a Module script inside a Script in the ServerScriptService which is simply a 1 line script that requires the ModuleScript:

require(script:WaitForChild('ModuleScript'))
0
From what I know about ModuleScripts, they do not run like regular scripts. You should put this in a regular script. hiimgoodpack 2009 — 6y
0
You made one of the worst datastore mistakes. You need to wrap ALL datastore calls inside a pcall() in case they error. lukeb50 631 — 6y
0
@lukeb50 How do I wrap it all in a pcall()? Please may you re-write the code as to what you'd put it as? Knucklehead_Ninja 4 — 6y

1 answer

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
6 years ago

As luke said, pcall. You probably didn't enable in-studio api's either. pcall is this:

pcall(function()

end)

Any errors inside this will not halt the script, and rather will cancel the pcall. If we build on that,

local Success, Val = pcall(function()
    return Value * 2
end)

We can detect if it was successful or not. 'Val' will also be Value * 2, as there is a return. I hope this helped you a bit.

Ad

Answer this question