DataStores Help

Ok, this has to be a simple fix, but I have been searching around for about an hour looking for something or someone to help me out.

What have you tried so far?

I have asked for help in two different discord servers. Only got a few people responding, and none of there solutions working.

What is the issue?

Here is my script.

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 XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderstats
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Parent = leaderstats
	
	local playerUserId = "Player_"..player.UserId
	
	--Local Data
		
	local data
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
		
	if success then
		XP.Value = data[1]
		Level.Value = data[2]
		--Set the data equal to the current XP
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		XP = player.leaderstats.XP.Value;
		Level = player.leaderstats.Level.Value;				
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
	if success then
		print("Data saved")	
	else
		print("Error in saving data")
		warn(errormessage)
	end
	
end)

I was getting an error but now I am not, I haven’t changed my script. I am so confused.

1 Like

“myDataStore” seems like a common datastore name, maybe you have used it before and data you are reading is not what you expected?

Also, you have showed your code but still have not explained the problem. If you don’t know what the problem is, maybe you could print debug to see where your code stops working.

I am pretty sure there is a problem about right here


It might be the fact that the table hasn’t been defined, but yet again, I am not getting anymore errors :woman_shrugging:

I think because you’re setting a dictionary, you would have to get the data by doing

XP.Value = data["XP"]
Level.Value = data["Level"]

OR you could just save the values as

local data = {
	player.leaderstats.XP.Value;
	player.leaderstats.Level.Value;				
}

and set the Async like this.
Make sure the game has access to API Services when in studio by configuring the game settings.

Still not working, I have Studio Access to API enabled and I changed the table to what you said. No errors in the output. Let me try the second thing.

Ok the DataStore still doesn’t work.

Is the script in a correct place like ServerScriptService?

Yep, it is inside ServerScriptService

Hi, I think it’s worth checking whether the initial GetAsync is nil (from someone who has never saved), then you can set both leaderstats to 0 and SetAsync() it. Also, add some prints after each pcall success and see the output to see if your code is even running.

1 Like

That doesn’t work, go look up datastore 2 and it better

I am learning programming, so I would rather stick with this right now. DataStore2 isn’t the only DataStore technique that works.

Personally, I would strongly recommend against learning DataStore2 (a DataStoreService wrapper) before learning DataStoreService. It’s not practical to forgo or delay the gaining of knowledge of a crucial API in it’s normal form.

Ok so I added some prints and basically it got to line 40 and then there were no more prints


Some where under Line40
image
There is probably a better way to test but I just added some prints inside functions. Also I should have mentioned “Data saved!” doesn’t print.

btw I just learned about DataStores today, but I read about it in the Developer Hub and watch a good tutorial video on it.

That said, DataStore2 brings some good performance benefits and shortens code a lot in my experience. But learn DataStoreService first.

i hear you but, his script came with XP, you need increment for that.

i have my own datastore2 and it work.

I know you can use the regular DataStore for this. DataStore2 isn’t necessary, but I know the benefits of it.

This DataStore script isn’t for a game or serious development, I just am trying to learn it, and for some reason it won’t work.

Well the question is, how you gonna fix his problem?

I’m not saying it doesn’t work, I’m saying that the focus should be on learning DataStoreService first. Also IncrementAsync() exists on DSS as well as DS2 (DataStore2:Increment())