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

Data Store Script Doesn't Save Data. Help?

Asked by
LuaDLL 253 Moderation Voter
5 years ago

It will not save any data at all. It prints no errors at all.

-- Variables
local Players = {};
local NumPlayers = #Players;
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local Assets = ReplicatedStorage:WaitForChild("Assets");
local Rounds = Assets:WaitForChild("Rounds");
local LavaRise = Rounds:WaitForChild("LavaRising");
local Stand = LavaRise:WaitForChild("Stand");
local PlayerStats = Assets:WaitForChild("Player_Stats");
local DataStoreService = game:GetService("DataStoreService");
local DataStore = DataStoreService:GetDataStore("MiniGamesV1");

local GameRounds = {"LavaRising",};

-- Functions/Main Game
function addPlayer(Player)
    table.insert(Players,Player);
end

function removePlayer(Player)
    for i,v in pairs(Players) do
        if v == Player then
            table.remove(Players,v);
        end
    end
    local Stats = PlayerStats:FindFirstChild(Player.Name);
    Stats:Destroy();
end

function chooseRound()
    local random = math.random(1,#GameRounds);
    local Picked = GameRounds[random];
    return Picked;
end

function LavaRising(Map,Lava,Stands)

end

function Joined(Player)
    addPlayer(Player); -- Adds player to the table "Players"
    local Stats = Instance.new("Folder");
    Stats.Name = Player.Name;
    Stats.Parent = PlayerStats;
    local Wins = Instance.new("NumberValue");
    Wins.Name = "Wins";
    Wins.Parent = Stats;
    Wins.Value = 0;
    local Coins = Instance.new("NumberValue");
    Coins.Name = "Coins";
    Coins.Parent = Stats;
    Coins.Value = 1000;
    local XP = Instance.new("NumberValue");
    XP.Name = "XP";
    XP.Parent = Stats;
    XP.Value = 0;
    local userdata;
    pcall(function()
        userdata = DataStore:GetAsync("!"..Player.userId);
    end)
    if userdata then
        warn("Got "..Player.Name.."'s data!");
        Wins.Value = userdata.Wins;
        Coins.Value = userdata.Coins;
        XP.Value = userdata.XP;
    else
        warn(Player.Name.." has no saved data yet!");
    end
end

function leave(Player)
    removePlayer(Player); -- Removes player from table "Players"
    local Stats = PlayerStats:FindFirstChild(Player.Name);
    local wins = Stats.Wins;
    local coins = Stats.Coins;
    local xp = Stats.XP;
    local StatTab = {Wins = wins.Value,Coins = coins.Value,XP = xp.Value};
    local userdata;
    pcall(function()
        userdata = DataStore:GetAsync("!"..Player.userId);
    end)
    if userdata then
        DataStore:UpdateAsync("!"..Player.userId,StatTab);
    else
        DataStore:SetAsync("!"..Player.userId,StatTab);
    end
end

game.Players.PlayerAdded:Connect(Joined);
game.Players.PlayerRemoving:Connect(leave);
0
You should be saving data periodically, and when the player leaves. There is a common chance that the server doesn't save the player's data when they leave, especially if they are the last player in a server, the server usually closes down before the datastore can set or update the record. It is also good to save periodically in case of shutdowns or connect loses. And by periodically I mean every climethestair 1663 — 5y

Answer this question