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

DataStore system not saving when PlayerRemoved??

Asked by 4 years ago
Edited 4 years ago

MODULE

local Module = {Data = {}}

local DataStoreService = game:GetService("DataStoreService");

local KeyPrefix = "data_user#";
local DataStore = DataStoreService:GetDataStore("TestData_v.15");

local DataTemplate = {
    Money = 300;
    Level = 1;
};

local function GetKey(id)
    return KeyPrefix .. id;
end;

Module.InitializeData = function(plr)
    local Key = GetKey(plr.UserId);
    Module.Data[plr] = DataStore:GetAsync(Key) or DataTemplate;
end;

Module.ConcludeData = function(plr)
    local Key = GetKey(plr.UserId);
    local Data = Module.Data[plr];
    if Data and Data ~= nil then
        DataStore:SetAsync(Key, Data);
        Data = nil;
    end;
end;

return Module;

SCRIPT

local DataModule = require(script.Parent:WaitForChild("DataModule"));

game.Players.PlayerAdded:Connect(function(Player)
    DataModule.InitializeData(Player);
end);

game.Players.PlayerRemoving:Connect(function(Player)
    DataModule.ConcludeData(Player);
end);

This is what I'm having trouble with. When in game and the data is changed, it works n all, but when i leave and rejoin, i have the default data.. It's tough because this used to be my method and worked like 100% of the time, and it just recently stopped.. Roblox did an update on studio not too long ago, could it be that??

0
hey wht is there ";" at the end of each line? TheRealPotatoChips 793 — 4y
0
idk, i kinda just picked it up and its a habit at this point ImperiumDomini2 39 — 4y
0
it doesn't affect the script though, those have always been there ImperiumDomini2 39 — 4y
0
Try to do a BindToClose waiting for the data save. yHasteeD 1819 — 4y
View all comments (4 more)
0
I tried that.. Roblox must've done something that messed with it because all my other datastores (in different games) aren't saving either... ImperiumDomini2 39 — 4y
0
Can you show the script complete? yHasteeD 1819 — 4y
0
Edited, should show it all ImperiumDomini2 39 — 4y
0
After testing, I've found that it pretty much works in-game, but studio is a flop when it comes to working w/ it. (API thingy enabled in studio also so idrk what it is) It's gonna be a little annoying to have to test a lot in-game but oh well, at least it works! Thank y'all for the help! ImperiumDomini2 39 — 4y

Answer this question