Help with DataStore

Hello, I am trying to make a Simple Datastore that I can Configure Easily to change with the world(Such as Roblox APIs). The Current Script I have is just a Copy of @Alvin_Blox 's DataStore Tutorial. The Problem is that it will not save the Data
Here is the code I am Using:

local datastore = game:GetService("DataStoreService")

local mistedge = datastore:GetDataStore("MistEdgeDataStore")


game.Players.PlayerAdded:Connect(function(player)
 	local stats = Instance.new("Folder")
	stats.Parent = player
	stats.Name = "leaderstats"
	local shillings = Instance.new("IntValue")
	shillings.Name = "Shillings"
	shillings.Parent = stats
	
	local success, errormsg = pcall(function()
		data = mistedge:GetAsync("User-"..player.UserId)
	end)
	if success then
		print("Data is has loaded with "..data)
		shillings.Value = data
	else
		warn(player.UserId.." User Data Not Retrieved, Error:"..errormsg)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data1 = player.leaderstats.Shillings.Value
	local success, errormsg = pcall(function()
	mistedge:SetAsync("User-"..player.UserId, player.leaderstats.Shillings.Value) 
	end)
	if success then
		print("Data Saved Successfully. Data:")
		print(data1)
	else
		print("Data Not Saved!")
		warn(player.UserId.." Data Save Error:"..errormsg)
	end
end)

The Pcall is still working and says everything is a success. Whenever I change the Value of the Stat and then exit the game(Console calls a Successful Save) and then re enter it won’t show the changed value.(And Yes, I have Roblox API Services activated for the game)
If you can help me out with this script, it would mean a lot!

PLEASE DO NOT GIVE ME AN ENTIRE SCRIPT, JUST GIVE ME A SIMPLE ANSWER OR POINT ME IN THE RIGHT DIRECTION

You have spelt SetAsync wrong.

1 Like

Ah, Thank you!
[30 characters]

Uh, It won’t Save Data Now, Like at all. The Pcall is still working and says everything is a success. Whenever I change the Value of the Stat and then exit the game(Console calls a Successful Save) and then re enter it won’t show the changed value.(And Yes, I have Roblox API Services activated for the game)

But is your issue that it’s not saving or is it actually not loading

It is Not Loading the saved Data, or it won’t save the value

[30 Characters]

I don’t think you need the second parameter in the GetAsync()

player.leaderstats.Shillings.Value

Yes, It has to be there due to the "User-“player.UserId” is the Key for the Table and the “player.leaderstats.Shillings.Value” is the Value of the Entry and the Problem is that it is not retrieving the Data to store it correctly. After Setting it to 10 and saving, it saves as 0

If I was to make a guess on why your Datastore isn’t working I would have to guess. That the GetAsync() Part isn’t right. I don’t think you need to add the “player.leaderstats.Shillings.Value” to it…

Just Fixed it to that and works the same as it is. It will not save the Data I Provide Correctly. Let me Update the Script Above.

are you testing this in studio or in-game?

Studio, In my testing Place I used a Diffrent Script and It worked and tried to move the same script to my new one it just doesn’t save data.

Is your Studio Access to API Services enabled?

Yes
[30 characters, boom, chicka boom]

Why don’t you just do “shillings.Value = mistedge:GetAsync(“User-”…player.UserId)” after the “shillings.Parent = stats”?

It works but the Main Problem is it Retrieving the Current Data of the Value and Adding it to the Data Table. So right now it only Saves 0 instead of the value I gave it(Such as 10). I think it is a SetAsync Issue. Should I try UpdateAsync?

1st bug:

If there is no previous data in the slot you’re trying to get the data out of such as “User-userId” (If you’re a new player or never played your game before) then it will return a nil, but will not error, you need to check if It’s a nil before continuing, not if the retrieval (GetAsync) was successful.

Using this instead of the 1st pcall function will prevent that and store some data in the slot if you’re a new player

local success, errormsg = pcall(function()
	data = mistedge:GetAsync("User-"..player.UserId)
	if data == nil then
		mistedge:SetAsync("User-"..player.UserId,0)
		data = 0
	end
end)

It is Still Saving as 0 Instead of the value I change the Variable to.

That is something on your end, I’m using the code you’ve provided and added them few lines I showed then and It’s working perfectly normal, my data is being saved and when I leave and join it will be loaded.

The code I’m using:

local datastore = game:GetService("DataStoreService")

local mistedge = datastore:GetDataStore("MistEdgeDataStore")


game.Players.PlayerAdded:Connect(function(player)
 	local stats = Instance.new("Folder")
	stats.Parent = player
	stats.Name = "leaderstats"
	local shillings = Instance.new("IntValue")
	shillings.Name = "Shillings"
	shillings.Parent = stats
	
	local success, errormsg = pcall(function()
		data = mistedge:GetAsync("User-"..player.UserId)
		if data == nil then
			mistedge:SetAsync("User-"..player.UserId,0)
			data = 0
		end
	end)
	if success then
		print("Data is has loaded with "..data)
		shillings.Value = data
	else
		warn(player.UserId.." User Data Not Retrieved, Error:"..errormsg)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data1 = player.leaderstats.Shillings.Value
	local success, errormsg = pcall(function()
	mistedge:SetAsync("User-"..player.UserId, player.leaderstats.Shillings.Value) 
	end)
	if success then
		print("Data Saved Successfully. Data:")
		print(data1)
	else
		print("Data Not Saved!")
		warn(player.UserId.." Data Save Error:"..errormsg)
	end
end)

How are you Changing the Variable? I’m just going through the Explorer and Changing it manually. And Are you running with the new Lua VM[IN BETA]