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

How can i make something save until the player joins back?

I'm trying to create a currency system where it gives you the amount of coins you entered + your existing ones, so say i put in like 54 the textlabel's text would do 0+54 and read 54 but then when you leave and re-join its back to 0

the ammount you have is put in is:

game.starterGui.SquidCoins.Frame.TextLabel.Text

where you put how many coins you want is in:

game.starterGui.SquidCoins.TextBox.Text

1 answer

For this you need to use Datastores. Put this in a normal script in ServerScriptService:

local DataStoreService = game:GetService("DataStoreService")
 
local experienceStore = DataStoreService:GetDataStore("SquidCoins")

game.Players.PlayerRemoving:Connect(function(plr) --When a player is leaving
	local success, errorMessage = pcall(function()
		experienceStore:SetAsync(plr.UserId, plr.PlayerGui.SquidCoins.Frame.TextLabel.Text)
	end)
	--It saves data when a player is leaving
	if not success then
		print(errorMessage)
	end
end)

game.Players.PlayerAdded:Connect(function(plr) --When a player joins
	local success, currentExperience = pcall(function()
		return experienceStore:GetAsync(plr.UserId)
	end)
	--it loads saved data when player joins again
	if success then
		plr.PlayerGui.SquidCoins.Frame.TextLabel.Text = currentExperience
	end
end)

Know more about Datastores here: Datastores - Roblox Developer Hub

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