The data script below works all well but refuses to load. It doesn't show any errors in output! Any idea whats going on? lmk thx
The issue is that the data wont load correctly! pls help
local data = require(script.DataModule) local DSS = game:GetService("DataStoreService") local statSave = DSS:GetDataStore("stats_Test2020") local settingSave = DSS:GetDataStore("settings_Test2020") local careerSave = DSS:GetDataStore("careers_Test2020") --ughh its not working game.Players.PlayerAdded:Connect(function(player) local key = "Data-ID"..player.UserId local statData = statSave:GetAsync(key) local settingData = settingSave:GetAsync(key) local careerData = careerSave:GetAsync(key) local StatsFolder = Instance.new("Folder", player) local SettingsFolder = Instance.new("Folder", player) local CareersFolder = Instance.new("Folder", player) StatsFolder.Name = "leaderstats" SettingsFolder.Name = "Settings" CareersFolder.Name = "Careers" ----- for i,stats in pairs(data["Stats"]) do local thing = Instance.new("IntValue", StatsFolder) thing.Name = tostring(stats) thing.Value = 0 end for i,setting in pairs(data["Settings"]) do local thing = Instance.new("BoolValue", SettingsFolder) thing.Name = tostring(setting) end for i,career in pairs(data["Careers"]) do local thing = Instance.new("BoolValue", CareersFolder) thing.Name = tostring(career) end if statData then for i,v in pairs(StatsFolder:GetChildren()) do v.Value = statData[i] end else local StatTable = {} for i,v in pairs(StatsFolder:GetChildren()) do table.insert(StatTable, v.Value) end statSave:SetAsync(key, StatTable) end if settingData then for i,v in pairs(SettingsFolder:GetChildren()) do v.Value = settingData[i] end else local SettingsTable = {} for i,v in pairs(SettingsFolder:GetChildren()) do table.insert(SettingsTable, v.Value) end settingSave:SetAsync(key, SettingsTable) end if careerData then for i,v in pairs(CareersFolder:GetChildren()) do v.Value = careerData[i] end else local CareersTable = {} for i,v in pairs(CareersFolder:GetChildren()) do table.insert(CareersTable, v.Value) end settingSave:SetAsync(key, CareersTable) end end) game.Players.PlayerRemoving:Connect(function(player) local key = "Data-ID"..player.UserId local Stats = player.leaderstats local Settings = player.Settings local Careers = player.Careers local statsTable = {} local settingsTable = {} local careersTable = {} for i,v in pairs(Stats:GetChildren()) do table.insert(statsTable, v.Value) end for i,v in pairs(Settings:GetChildren()) do table.insert(settingsTable, v.Value) end for i,v in pairs(Careers:GetChildren()) do table.insert(careersTable, v.Value) end statSave:SetAsync(key, statsTable) settingSave:SetAsync(key, settingsTable) careerSave:SetAsync(key, careersTable) end)
I just skimmed through your script, but you said everything was working fine except for the data not saving. I know a lot of people have this problem too. Check out this article. Hopefully this answers your question. Basically, the server may shut down before the last player leaves or if you're playing Solo in the Studio, thus not being able to complete all the functions. To fix that, use BindToClose
. Place the function that you want to run before the server shuts down, inside of the BindToClose
function. The function should be completed less than 30 seconds at most before the server shuts down.
game:BindToClose(function() -- Runs the PlayerRemoving function before server shuts down for i, player in pairs(game.Players:GetPlayers()) do local key = "Data-ID"..player.UserId local Stats = player.leaderstats local Settings = player.Settings local Careers = player.Careers local statsTable = {} local settingsTable = {} local careersTable = {} for i,v in pairs(Stats:GetChildren()) do table.insert(statsTable, v.Value) end for i,v in pairs(Settings:GetChildren()) do table.insert(settingsTable, v.Value) end for i,v in pairs(Careers:GetChildren()) do table.insert(careersTable, v.Value) end statSave:SetAsync(key, statsTable) settingSave:SetAsync(key, settingsTable) careerSave:SetAsync(key, careersTable) end end)
I had this problem for a while and i figured Values inside of player when updated don't update for the server(or something like that idk). the way i worked around this was i had a remote send the data from a local script to the server when it was called.
example...
Client:
local remote = <remote> local data = <value thingy in player>.Value local function save() remote:FireServer(data) end
Server:
local remote = <remote> remote.OnServerEvent:Connect(function(player, data) Datastore:SetAsync(key, data) end)
idk but it works