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

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

Asked by 4 years ago
Edited 4 years ago

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

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. killerbrenden 1537 — 4y
0
Sorry, i dont seem to understand. I'm concating the PlayerUserId, like the following: maxpax2009 340 — 4y
0
Could you use Lua Code Blocks in the formatting section of the question, so it's easier to read? killerbrenden 1537 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Thanks dude, i could not have done it without your help! maxpax2009 340 — 4y
0
I'm glad I could help! DizzyGuy70 38 — 4y
Ad

Answer this question