Save data script not working

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

game.Players.PlayerAdded:Connect(function(plr)
	local key = "user_"..plr.UserId

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

	local GetSaved
	local sucess, warnmsg = pcall(function()
		GetSaved = ds:GetAsync(key)
	end)
		if GetSaved and sucess then
			money.Value = GetSaved
			print("loaded data")
		elseif warnmsg then
			warn(warnmsg)
		else
			money.Value = 10
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local money = plr:WaitForChild("PlayerGui"):WaitForChild("Money").TextLabel.Money
	print("saving data for player "..plr.Name)
	local sucess, warnmsg = pcall(function()
		ds:SetAsync("user_"..plr.UserId, money.Value) 
	end)
	if sucess then
		print("saved data")
		print(money2.Value)
	else
		warn(warnmsg)
	end
end)

I don’t think that this edited script will fix it but I guess you might as well try. Also do you use multiple datastores with the same name? It seems like you didn’t change the name on line 1.

By the way, keep the game:BindToClose thing I mentioned earlier. You can remove it later once the script is working.

1 Like

for the thing on line 1 I have no idea what Im doing lol

it prints data loaded but doesnt seem to actually stay true to that message

I will probably be afk for a bit Ive been trying to fix this stupid problem for 4 hours lol

thanks for the help tho

2 Likes

local ds = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
Just change it to this for now in case you have other ones with the same name. By the way, are you updating the text after the data loads?

1 Like

yes I use this to update it:

while true do
	script.Parent.Text = "Money: " ..tostring(script.Parent.Money.Value)
	wait(0.1)
end
script.Parent.Money.Changed:Connect(function()
	script.Parent.Text = "Money: " ..tostring(script.Parent.Money.Value)
end)

ill stop replying to you every 10 seconds now lol

1 Like

You can try one of these here I hope it will work:

  1. Remove the wait() function as it is unnecessary.

  2. Remove the unnecessary print statement in the else block.

  3. Correct the parameters of the GetAsync function. It should only have one parameter (the key) and not two.

  4. Remove the unnecessary brackets around savevalue1.Value in the SetAsync function.

  5. Update the bindToClose function to iterate through

game.Players:GetPlayers() instead of
game.Players:GetChildren().