Recently I’ve been working on a datastore for a game, but whenever I leave the game, data fails to save and I get no error. I’m not sure what I’ve been doing wrong despite looking over it multiple times.
API services are enabled and I’ve tried using BindToClose and PlayerRemoving just to get problems with both of them.
local playerId = "Player_"..player.UserId
--// LOADING DATA
print("LoadingData...")
local success, errormessage = pcall(function()
datasave = DataStore:GetAsync(playerId, datasave)
end)
if success then
print("Loaded data!")
player.Data.Alive.Value = datasave.alive
player.Data.SwordEXP.Value = datasave.swordEXP
end
game.Players.PlayerRemoving:connect(function(player)
local playerId = "Player_"..player.UserId
print(playerId)
local datasave = {
swordEXP = player.Data.SwordEXP.Value;
igname = player.Data.IGName.Value;
race = player.Data.Race.Value;
alive = player.Data.Alive.Value;
fistexp = player.Data.FistEXP.Value;
}
local success, errormessage = pcall(function()
DataStore:SetAsync(playerId, datasave)
end)
if success then
print("Data saved")
else
print("Data did not save")
end
end)
local playerId = "Player_"..player.UserId
--// LOADING DATA
print("LoadingData...")
local success, errormessage = pcall(function()
return DataStore:GetAsync(playerId) -- just use the key, it doesn't take a second argument
end)
if success then
if errormessage then -- "errormessage" would be whatever is returned, if an error occurs "success" would be false and everything in it's scope won't run
-- this is also to check if the player has data
print("Loaded data!")
player.Data.Alive.Value = errormessage.alive
player.Data.SwordEXP.Value = errormessage.swordEXP
else
print("player had no data to load")
end
else
warn("unable to get data:", errormessage) -- tells you the issue
end
maybe this is the issue, are you running this in studio?
I was running it in studio, but I tried switching to that code and it always gives me the MSG that data didn’t save, I’ll try running the script ingame
local playerId = "Player_"..player.UserId
--// LOADING DATA
print("LoadingData...")
local success, errormessage = pcall(function()
return DataStore:GetAsync(playerId) -- just use the key, it doesn't take a second argument
end)
if success then
if errormessage then -- "errormessage" would be whatever is returned, if an error occurs "success" would be false and everything in it's scope won't run
-- this is also to check if the player has data
print("Loaded data!")
player.Data.Alive.Value = errormessage.alive
player.Data.SwordEXP.Value = errormessage.swordEXP
else
print("player had no data to load")
end
else
warn("unable to get data:", errormessage) -- tells you the issue
end
game.Players.PlayerRemoving:connect(function(player)
local playerId = "Player_"..player.UserId
print(playerId)
local datasave = {
swordEXP = player.Data.SwordEXP.Value;
igname = player.Data.IGName.Value;
race = player.Data.Race.Value;
alive = player.Data.Alive.Value;
fistexp = player.Data.FistEXP.Value;
}
local success, errormessage = pcall(function()
DataStore:SetAsync(playerId)
end)
if success then
print("Data saved")
else
print("Data did not save")
end
end)