DataStore not working

This category should only be used for open discussions on Roblox development!

Please use #help-and-feedback:game-design-support if you need help implementing specific game features. ``` local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local playerUserId = "Player_"..player.UserId

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)

if success then
	Cash.Value = data
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Cash.Value

local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print("Data successfully saved!")
else
	print("There was an error!")
	warn(errormessage)
end

end) ``` Theres no error, but its not saving either

It didnt print out the success either though

Hey there!

Here, try these:

  1. Make sure that you have HTTP requests on.
  2. Try debugging your script
  3. Check your syntax
  4. Make sure you are doing it in Roblox Player as it won’t work in studio. (Which I wish they would change but that’s besides the point.)
  5. If none of these work, post your code.

Do you have studio access to api services enabled (which allows datastores to work in studio)?

1 Like

Probably shows no error because the problem happens on this line, where the player is leaving the game. Use two dots to concatenate instead of 3 dots.

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”..player.UserId

I tried all of those things, and it still doesn’t work
My code is here

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“myDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats

local playerUserId = "Player_"..player.UserId
-- Load Data

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)

if success then
	Cash.Value = data
	-- Set our data equal to the current Cash
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Cash.Value

local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print("Data successfully saved!")
else
	print("There was an error!")
	warn(errormessage)
end

end)

1 Like

Sorry that was just a typo, i checked my original script, and it had 2 dots, idk why it showed up as 3 dots there

You don’t really need to worry about HTTPRequests when messing with datastores, only with API Studio Acess

I know. Better be safe than sorry I suppose.

P.S. I meant to say API.

Nah, HTTP Requests aren’t linked to API requests from Roblox, in fact a lot of stuff you mess with HTTPService is limited to stuff outside roblox. You can’t even do a request from roblox to roblox. Unless you’re using a proxy, anyways, datastores are roblox services and they’re only accessible through roblox itself. Hmm…

You said the same on another post but ok :P


To the original poster:

Please format your code correctly. Don’t use the quotes icon to format it there’s one which is a </> icon. Use that instead. Its very hard to read it.

Btw I don’t see anything wrong with your code so…

You’re my kind of guy. Thanks for letting me know.

1 Like

Stop spreading misinformation, you’re part of the problem if you add to it like that.

@OP, you need to enable API services in settings and also account for players currently in game that join before PlayerAdded can fire.

3 Likes

yes true, there is a option that you can turn on that enables to make dataStores in studio work as @LordMerc said “Stop spreading misinformation”

Make sure to check if you have data or no, it makes debugging easier.

if success then
   if data then -- make sure to always check it inside the if success statement
      warn("data found")
      Cash.Value = data
   else
      warn("data not found")
   end
else
   warn("failed to load")
end

If you still can’t load or save, make sure to change the data store name “MyDataStore” to something else to reset your data.

I’d recommend to do this instead:

local Cash = player.leaderstats.Cash
local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, Cash.Value)
end)