Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My DataStoreService wont save the data when i leave the game?

So, for a brief backround... I tried making a DataStore that would save my total deaths. But it does not work?

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
 local leaderstats = Instance.new("Folder", player)
 leaderstats.Name = "leaderstats"
 
 local Deaths = Instance.new("IntValue", leaderstats)
 Deaths.Name = "Deaths"
 
 player.CharacterAdded:Connect(function(char)
  Deaths.Value = Deaths.Value + 1
  
  if Deaths.Value == 500 then
   print("You leveled up!")
  end
  
  local PlayerUserId = "Player_"..player.UserId
  
  -- Load Data
  
  local Data
  local sucess, errormessage = pcall(function()
   Data = myDataStore:GetAsync(PlayerUserId)
  end)
  
  if sucess then
   -- Setting our data equal to the current cash
   Deaths.Value = Data
  end
  
  
  
 end)
 game.Players.PlayerRemoving:Connect(function(player)
  local PlayerUserId = "Player_"..player.UserId
  
  -- Setting the data
  
  local data = player.leaderstats.Deaths.Value
  
  local sucess, errormessage = pcall(function()
  myDataStore:SetAsync(PlayerUserId, data)
  end)
  if sucess then
   print("Data succesfully saved")
  else
   print("Error occured whilst saving leaderstats...")
   warn(errormessage)
  end
 end)
 end)


And yes i have enabled API services

3 comments

0

You're not saving as the same key value. In loading your doing Player_1, and in the saving your doing 1. You need to use the same Player_playerUserId thing you have going.

0

Sorry, i dont seem to understand. I'm concating the PlayerUserId, like the following:

0

Could you use Lua Code Blocks in the formatting section of the question, so it's easier to read?

1 answer

✓ Accepted answer

If you're attempting to know if the DataStore is saving or loading the player's data in Roblox Studio, then it wouldn't save it. It would just load the player's data.

DataStoreService doesn't fully function on Roblox Studio so I suggest you try it on a normal server, not in Roblox Studio.

By the way, I know this isn't related to you're problem but you should be concerned about DataStore limits. If the game exceeds the DataStore limit then the Roblox engine will automatically throttle the game's data store usage, causing data requests to take longer.

Click here to learn more about DataStore limits

2 comments

0

Thanks dude, i could not have done it without your help!

0

I'm glad I could help!

This discussion is preserved as it was. New answers, comments, and votes are closed.