the datastore does not always save, and when the player reEnters the game, it does not even Retrieve data.
local DataService = game:GetService("DataStoreService") local DataStore = DataService:GetDataStore("Doesnnotd Matterrigh now") game.Players.PlayerAdded:Connect(function(player) local days = player:WaitForChild("leaderstats").Days local Days_Data local success, errorMessage = pcall(function() Days_Data = DataStore:GetAsync(player.UserId.. "days") --(key, keyword) --you should make it a keyword, rather than a value end) if success then days.Value = Days_Data print("Retrived") elseif errorMessage then warn(errorMessage) end end) game.Players.PlayerRemoving:Connect(function(player) local days = player.leaderstats.Days local success, errorMessage = pcall(function() DataStore:SetAsync(player.UserId.. "days", days.Value) --(key, keyword, value) end) if success then print(player," Has Left") print("All Saved") print(days.Value) else warn(errorMessage) --warn is like print, except in orange text, showing where the warning is end end)
I think the problem might be the fact when the data is being retrieved for the very first time, GetAsync
returns nil
.. your code doesn't check to make sure its not nil.. when you set days.Value
to a nil value, it will halt the code indefinitely.. so do something like this:
local CollectionService = game:GetSerice("CollectionService") local DataService = game:GetService("DataStoreService") local DataStore = DataService:GetDataStore("Doesnnotd Matterrigh now") game.Players.PlayerAdded:Connect(function(player) local days = player:WaitForChild("leaderstats").Days local Days_Data local success, errorMessage = pcall(function() Days_Data = DataStore:GetAsync(player.UserId.. "days") --(key, keyword) --you should make it a keyword, rather than a value end) if success then days.Value = Days_Data or 0 CollectionService:AddTag(days, "data_loaded") print("Retrived") elseif errorMessage then warn(errorMessage) end end) game.Players.PlayerRemoving:Connect(function(player) local days = player.leaderstats.Days if(not CollectionService:HasTag(days, "data_loaded") then --this ensures data is saved only is it has been loaded from datastore, to avoid data loss return end local success, errorMessage = pcall(function() DataStore:SetAsync(player.UserId.. "days", days.Value) --(key, keyword, value) end) if success then print(player," Has Left") print("All Saved") print(days.Value) else warn(errorMessage) --warn is like print, except in orange text, showing where the warning is end end)
Maybe you have API Service disabled? Also, DataStore doesn't work inside Roblox Studio.
If you go to "Game Settings" in the "Home Tab", then go to "Options", and "Enable Studio API services", and set that to true. Your place must also be published.
Go to game settings Then turn on Enable Studio API
try this
-- Set up table to return to any script that requires this module script local PlayerStatManager = {} local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData") -- Table to hold player information for the current session local sessionData = {} local AUTOSAVE_INTERVAL = 60 -- Function that other scripts can call to change a player's stats function PlayerStatManager:ChangeStat(player, statName, value) local playerUserId = "Player_" .. player.UserId assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match") if typeof(sessionData[playerUserId][statName]) == "number" then sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value else sessionData[playerUserId][statName] = value end end -- Function to add player to the "sessionData" table local function setupPlayerData(player) local playerUserId = "Player_" .. player.UserId local success, data = pcall(function() return playerData:GetAsync(playerUserId) end) if success then if data then -- Data exists for this player sessionData[playerUserId] = data else -- Data store is working, but no current data for this player sessionData[playerUserId] = {Money=0, Experience=0} end else warn("Cannot access data store for player!") end end -- Function to save player's data local function savePlayerData(playerUserId) if sessionData[playerUserId] then local success, err = pcall(function() playerData:SetAsync(playerUserId, sessionData[playerUserId]) end) if not success then warn("Cannot save data for player!") end end end -- Function to save player data on exit local function saveOnExit(player) local playerUserId = "Player_" .. player.UserId savePlayerData(playerUserId) end -- Function to periodically save player data local function autoSave() while wait(AUTOSAVE_INTERVAL) do for playerUserId, data in pairs(sessionData) do savePlayerData(playerUserId) end end end -- Start running "autoSave()" function in the background spawn(autoSave) -- Connect "setupPlayerData()" function to "PlayerAdded" event game.Players.PlayerAdded:Connect(setupPlayerData) -- Connect "saveOnExit()" function to "PlayerRemoving" event game.Players.PlayerRemoving:Connect(saveOnExit) return PlayerStatManager
This problem also happened to me, the way I fixed it was by turning off the DataStore created by my admin commands I was using, and also double check that Studio has access to API Services by going to Game Settings, Options, and then turning the option on.