DataStore Problems

Hello, the names fried and I watched a tutorial video from the DevKing/Tapwater about datastores and I followed the whole video, when it didn’t save I was like ok my fault checked the output nothing, nothing was wrong it just wasn’t saving for some reason help me please, I tried every where, comments, youtube.

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 MoneyBag = Instance.new("IntValue")
	MoneyBag.Name = "MoneyBag"
	MoneyBag.Parent = leaderstats
	
	
	local UserId = "Player_"..player.UserId
	 
	local data
	local sucsess, errormessage = pcall(function()
		data = myDataStore:GetAysnc(UserId)
	end) 
	
	if sucsess then
		MoneyBag.Value = data
	end
	 	
	
end)




game.Players.PlayerRemoving:Connect(function(player)
	local UserId = "Player_"..player.UserId
	
	local data = player.leaderstats.MoneyBag.Value
	
	local sucsess, errormessage = pcall(function()
	myDataStore:SetAsync(UserId, data)	
	end)
	
	if success then
	print("Data Saved Successfully!")
	else
	if errormessage then
	print("Oh NO NO NO NO NO a Mistake Has Been Made")
	warn(errormessage)
			
		end	
	end 
end)

thank you for reading and peace,
fried

The reason is probably because u don’t have a BindToClose call anywhere.
U should loop trough all players and save their data in a BindToClose call.

And SetAsync should not be used to save Player Data.

U also spelled success incorrectly

where should I put the BindToClose? I did try to use it but had no idea where to put it

Somewhere outside of the events. The end of the script would probably be a good place

Sorry, I’m pretty new to this scripting thing.
what do you mean by outside the events

If u are new to Scripting, Datastore should not be touched with. DataStore requires a great amount of Lua knowledge

no, that’s not what I meant I just wrote it out wrong I’ve been scripting for 5-6 months now, so I have the basic knowledge I am also trying to make the BindToClose()
I doesn’t seem to be working

I can script a savable leaderboard, but I am not sure if that’s the only thing you need.

I wrote the BindToClose but I only get a red line under it

Here you go anyway:

local ds = game:GetService("DataStoreService"):GetDataStore("--MoneyBag01")

game.Players.PlayerAdded:Connect(function(player)
	local key = "moneybag-"..player.userId
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats" --Keep this lowercase
	local currency = Instance.new("IntValue",folder)
	currency.Name = "MoneyBag" --Just your currency I got from your script, you can change it to whatever
	currency.Value = 0 --The starting value for when the player first joins the game
	
	local save = ds:GetAsync(key)
	if save then
		currency.Value = save --Saving the amount the player got
	end
	currency.Changed:Connect(function()
		ds:SetAsync(key,currency.Value)
	end)
end)

that one dosen’t work letme write my script,

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

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

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


local UserId = "Player_"..player.UserId
 
local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAysnc(UserId)
end) 

if success then
	MoneyBag.Value = data
end

end)

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

local data = player.leaderstats.MoneyBag.Value

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

if success then
print("Data Saved Sucessfully!")
else
if errormessage then
print("Oh NO NO NO NO NO a Mistake Has Been Made")
warn(errormessage)
		
	end	
end 

end)

BTO(function ()
print(2*2) — Not Needed just there to make sure it prints
wait(3)
print(“Done”)
end)

1 Like

I use that scripts and it works fine, how did you test it and where did you place it exactly?

ServerScriptService, nope I even deleted my old one

You misspelled GetAsync

It should be Async. Apart from that and the success misspelling that others have pointed out, that should be everything.

does not work, I literally made sure there were no misspellings.
I’ll just re-write it

Updated Code

Spelling Errors Fixed, fixed your code.

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore") -- Your DataStore Name

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local MoneyBag = Instance.new("NumberValue", leaderstats)
	MoneyBag.Name = "MoneyBag"

	local data
	local IsSaved,DataStoreError = pcall(function()
		data = myDataStore:GetAsync(player.UserId) or 0
	end)
	local function Save(Player,Data) -- // Does a SaveFunction
		local CallBack = pcall(function()
			myDataStore:SetAsync(Player, Data)
		end)
		if CallBack then
			print('Data Saved')
		elseif not CallBack then
			error('Data Error '..tostring(CallBack))
		end
	end
	MoneyBag.Changed:Connect(function()
		Save(player.UserId, MoneyBag.Value)
	end)
	game.Players.PlayerRemoving:Connect(function(PlayerWhoLeft)
		Save(PlayerWhoLeft.UserId, MoneyBag.Value)
	end)
end)

I dont know if its me or my computer but the code only says it saves once or twice but it doesn’t save