Why my data store is not working?

I made a data store to save the player’s Money in the game but oddly it’s not working

Data Store script:

local DataStore = game:GetService("DataStoreService")
local DollarStore = DataStore:GetDataStore("MyDollars")

game.Players.PlayerAdded:Connect(function(plr)
	local Dollar = plr:WaitForChild("Dollar")
	local data
	local succ, err = pcall(function()
		data = DollarStore:GetAsync(plr.UserId.."_Dollar", plr.Dollar.Value)
	end)
	
	if succ then
		Dollar.Value = Dollar
		print("Successful")
	else
		print("Failed")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)


	local Dollar = plr:WaitForChild("Dollar").Value
	
	local succ, err = pcall(function()
		Dollar = DollarStore:GetAsync(plr.UserId.."_Dollar", plr.Dollar.Value)
	end)
	
	if succ then
		print("Success")
	else
		print("Failed")
	end
end)

And this is the AutoMoney script:

game.Players.PlayerAdded:Connect(function(plr)
	local value = Instance.new("NumberValue", plr)
	value.Name = "Dollar"
	
	while wait(60) do
		value.Value = value.Value + 5
	end
end)

What is the problem?

1 Like

This obviously, because you didn’t create the leaderstats.

1 Like

Hmm it seems your missing some things, do you have any programming experience if so I could give you a better/more efficient way of making a data store but it may require past knowledge of programming.

It’s “SetAsync” not “GetAsync” .

4 Likes

You aren’t even setting the datastore key so of course, it won’t save.

1 Like

Thank you let me test it again

If that doesn’t work I can give you some code, but I’d rather you’d learn how to do it instead of just copying and pasting code because you’ll remember it in the past if you do it your self instead of pasting code you don’t understand.

1 Like

I highly recommend doing task.wait(60) instead of just “wait”. “wait” will be deprecated soon, so its a good habit to start usng task.wait()

2 Likes

it will be gladly appreciated if you could teach

I actually am open to show you how to use data stores, do you have discord?

If not I can message you on the dev forum.

Thank you for your recommendation. Sure I will try using that but there is a question:

Is it better to replace “wait” with this in all different situations or only in this situation?

I have discord!

Yoshikage Arman#6690

In all situations. I can’t think of any other situation where you would choose wait() over task.wait()

1 Like

Okay! I’ll friend you I’m: I AM THE WORST GAMER #0332

2 Things that I’ve noticed off the bat is that,

local succ, err = pcall(function()
		data = DollarStore:GetAsync(plr.UserId.."_Dollar", plr.Dollar.Value)
	end)

This, you seem to put the Dollar Value in GetAsync, (You only need to put the key, as you’re Getting the Data not Setting the data) which won’t work.



	local succ, err = pcall(function()
		Dollar = DollarStore:GetAsync(plr.UserId.."_Dollar", plr.Dollar.Value)
	end)

here you seem to want to set the data when the player is leaving, So you’ll have to use SetAsync, not GetAsync.

1 Like
  1. You are setting the money value to the money object.
  2. GetAsync has one paramenter.
  3. You are using GetAsync when saving the player’s money.
  4. Use game:BindToClose() to save every player’s data when the game either shuts down or the last person leaves the server
1 Like
local DataStore = game:GetService("DataStoreService")
local DollarStore = DataStore:GetDataStore("MyDollars")

game.Players.PlayerAdded:Connect(function(plr)
	local Dollar = plr:WaitForChild("Dollar")
	local data
	local succ, err = pcall(function()
		data = DollarStore:GetAsync(plr.UserId.."_Dollar")
	end)
	
	if succ then
		Dollar.Value = data
		print("Successful")
	else
		print("Failed")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)


	local Dollar = plr:WaitForChild("Dollar").Value
	
	local succ, err = pcall(function()
		Dollar = DollarStore:SetAsync(plr.UserId.."_Dollar", plr.Dollar.Value)
	end)
	
	if succ then
		print("Success")
	else
		print("Failed")
	end
end)

Yes, please use BindToClose() to ensure that the player data is saved. There is a code sample here

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

No need I got him! Taught him about datastores on discord and fixed his issue!