Save data script not working

Hi Im trying to make a money value save but I am stuck rn

I keep getting this error:
image

This is the script:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
	wait()
	
	local key = "user_"..plr.UserId
	
	local money = plr.PlayerGui.Money.TextLabel.Money
	
	local GetSaved = ds:GetAsync(key)
	if GetSaved then
		money.Value = GetSaved
	else
		local money2 = money.Value
		print(money2)
		ds:GetAsync(key, money2)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local savevalue1 = plr.PlayerGui.Money.TextLabel.Money
	
	ds:SetAsync("user_"..plr.UserId, {savevalue1.Value})
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChildren()) do
		plr:Kick()
	end
end)
6 Likes

You are saving a table, not a number

{savevalue1.Value}

You should do:

ds:SetAsync("user_"..plr.UserId, savevalue1.Value)
3 Likes

ohh damn you right wait let me check if that works

2 Likes

ok that did nothing same error lol

1 Like

You tried to put the value of the money with a table, not a number
To fix this, do this:
money.Value = GetSaved[1]

also, you should use pcalls because datastore can get internal errors and it will break your script if there is no a pcall

1 Like

Your data has not been changed from a table to a number, maybe rejoining ll fix the problem

2 Likes

I rejoined like 3 times but nothing also I have no idea what pcalls are

1 Like

You only put 1 argument in for GetAsync.

ds:GetAsync(key)[1] -- 1 represents first index in table which is the money.
1 Like
local GetSaved = ds:GetAsync(key)
	if GetSaved then
		money.Value = GetSaved
	else
		local money2 = money.Value
		print(money2)
		ds:GetAsync(key, money2)
	end

The problem is at line ds:GetAsync(key, money2) your trying to pass “money2” as a argument when getasync only requires 1.
Im not sure why your calling getasync here but you can try this code

local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	local key = "user_"..plr.UserId

	local money = plr.PlayerGui.Money.TextLabel.Money

	local sucess, warnmsg = pcall(function()
		local GetSaved = ds:GetAsync(key)
		if GetSaved then
			money.Value = GetSaved
		else
			money.Value = 10 --some default value since player has no data
		end
	end)
	if warnmsg then
		warn(warnmsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local savevalue1 = plr.PlayerGui.Money.TextLabel.Money

	local sucess, warnmsg = pcall(function()
		ds:SetAsync("user_"..plr.UserId, savevalue1.Value) 
	end)
	if warnmsg then
		warn(warnmsg)
	end
end)

--removed the game bind to close because its unnecessary

I would also suggest doing waitforchild on playergui since it may not be loaded.

1 Like

well Im no longer getting the error but it still isnt saving and loading the data…

I have API enabled I seriously dont know whats the problem (I am testing this just in studio but it should work there too Im pretty sure)

2 Likes

Is there anything in output? The pcall catches the error

1 Like

Add a print statement after it saves to see if it was successful

	local sucess, warnmsg = pcall(function()
		ds:SetAsync("user_"..plr.UserId, savevalue1.Value) 
	end)
	if sucess then
		print("saved data")
		print(savevalue1.Value)
	else
		warn(warnmsg)
	end

If it isn’t printing anything, then use BindToClose to save the players data.

game:BindToClose(function()
	task.wait(5) --You can also cycle through every player in the game and save their data, but task.wait(5) should work for now.
end)

image

I tried using waitforchild but that just removed the error without actually fixing the saving

1 Like

It doesnt print anything even when I use bindtoclose

1 Like

Add a print statement before the pcall to see if the game is even trying to save the data. Also, this script is in ServerScriptService, right?

1 Like

yeah, also I did add it and it doesnt print anything

1 Like

Can you post the updated version of the script in case you made a typo somewhere? It is strange that the playerremoving block isn’t running at all.

By the way, this isn’t related to the issue but saving a player’s money inside of their Gui is a bad idea because any exploiter can just change their money value to whatever they want. Put their money value in the server or under their player’s instance (the one in game.Players, not in workspace)

1 Like

image

ok its just giving me this error now

also on the exploiters not Im really not concerned since money is mostly for cosmetic stuff so do what you want lol

On line 8, do
local money = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money

The gui isn’t loaded in the game yet, so the script is giving you an error.

By the way, I’m not sure if you need the WaitForChild for PlayerGui but I guess it doesn’t hurt to have it.

1 Like

now its back to not printing anything again while not working

heres the full script you asked for:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveName")

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	local key = "user_"..plr.UserId

	local money = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money

	local sucess, warnmsg = pcall(function()
		local GetSaved = ds:GetAsync(key)
		if GetSaved then
			money.Value = GetSaved
		else
			money.Value = 10 --some default value since player has no data
		end
	end)
	if warnmsg then
		warn(warnmsg)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local money2 = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money

	local sucess, warnmsg = pcall(function()
		ds:SetAsync("user_"..plr.UserId, money2.Value) 
	end)
	if sucess then
		print("saved data")
		print(money2.Value)
	else
		warn(warnmsg)
	end
end)

--removed the game bind to close because its unnecessary
1 Like