Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make a button that saves leaderstats into DataStores?

Hello everyone, I've read the Roblox Wiki on all of the items used below, but when the script is run, it returns no error and all print()'s are printed. Anyone know why it just refuses to save? :)

Button script:

local serversave = game.Workspace:WaitForChild("ServerSave")

function onClicked()
	script.Parent.Parent.Status.Text = "Saving..." -- just for looks
	print("Sending Save Request...")
	serversave:InvokeServer(player) -- triggering server script since button script is not a server script
	print("Sent Save Request!")
	wait(2) -- everything after this is just for looks
	script.Parent.Parent.moolah.number.Text = game.Players.LocalPlayer.leaderstats.Money.Value
	script.Parent.Parent.bb.number.Text = game.Players.LocalPlayer.leaderstats.BuildingBux.Value
	script.Parent.Parent.xp.TextLabel.Text = "EXP Level: "..game.Players.LocalPlayer.leaderstats.Level.Value
	script.Parent.Parent.Status.Text = "Saved!" -- This is just for me so that there were no errors occurring in the above
end
script.Parent.MouseButton1Click:connect(onClicked)

Save Script inside ServerScriptService:

local serversave = Instance.new("RemoteFunction") -- making the RemoteFunction
serversave.Name = "ServerSave"
serversave.Parent = game.Workspace

local DS = game:GetService("DataStoreService"):GetDataStore("leaderstats")

serversave.OnServerInvoke = function(player) -- run when button is pressed and the remote function is called(?)
	local key = "plr-"..player.userId
	
	local Money = player.leaderstats.Money
	local BB = player.leaderstats.BuildingBux
	local Level = player.leaderstats.Level
	
	print("Attempting to Server Save...")
	DS:SetAsync(key, {Money.Value, BB.Value, Level.Value}) -- table?
	print("Server Saved!") -- this does get printed and no errors are in output.
end

2 comments

0

How do you know it's not working?

0

^^ You might just not be loading it correctly.

1 answer

The data is saving properly, but you aren't loading it back when the player rejoins the game. Check out GetAsync to learn how to load in the saved data. You would need to load it as soon as the player joins using PlayerAdded and set the corresponding leaderstats to the saved data.

This discussion is preserved as it was. New answers, comments, and votes are closed.