How would I go about making a GUI button's visible data save

I want to save a GUI button’s visible state, for example:
If the player clicks the button it makes visible = false and when that player rejoins that button is still set to visible = false.

2 Likes

I’m not sure what Part you are having trouble with, You can use a ModuleScript for the DataStore, Have a Value that indicates its false,

For example:

Lets Say you have Data, you have a Key for if you clicked a Button, Lets Say this is your Data

SessionData[329059901] = { -- Yes, I went out of my way to get your UserId
    Level = 1,
    Cash = 100,
    ButtonClicked = false -- your Button Indicator

}

But with another, Script you need to Load The Data first before applting, so then you would need to send Data via a RemoteEvent so your Data can Save, if you do it on the Client, It wont Save as the Server wont Recognize that change.

if not DataStore.SessionData[player.UserId].ButtonClicked then -- If key is false
    Button.Visible = false -- Visibility is false
end
Button.Activated:Connect(function()
    Pressed = true -- Pressed
    RemoteEvent:FireServer(Pressed) -- Fires to Server
    wait() -- waits a moment
    Button.Visible = not DataStore.SessionData[player.UserId].ButtonClicked -- Sets Visibility
end)

In the RemoteEvent, you would do:

local DataStore = require(Module) -- ModuleScript
Event.OnServerEvent:Connect(function(plr) -- Event Connection
    DataStore.SessionData[plr.UserId].ButtonClicked = true -- Sets Key Value
end)

Use datastores, (Data Stores | Roblox Creator Documentation), make the datastore contents the value of the buttons visibility (Save whenever the player leaves the game or when the button is clicked). Upon loading the game check the datastore, and set the button’s visibility depending on the value returned by the datastore.

Here’s an example

Local Script (Child of button)

local button = script.Parent
local Remote = game.ReplicatedStorage.RemoteEventSave
local Remote2 = game.ReplicatedStorage.RemoteEventLoad

local Bool = Remote2:FireServer()
if Bool == true or Bool == false then
	button.Visible = Bool
else
	button.Visible = true
end

button.MouseButton1Click:Connect(function()
	button.Visible = not button.Visible
	Remote:FireServer(button.Visible)
end)

Server Script (Child of ServerScriptService)

local Remote = game.ReplicatedStorage.RemoteEventSave
local Remote2 = game.ReplicatedStorage.RemoteEventLoad
local DSS = game:GetService("DataStoreService")

Remote.OnServerEvent:Connect(function(Player, Bool)
	local Data = DSS:GetDataStore(Player.UserId.."ButtonVisibility")
	local Success, Error = pcall(function()
		Data:SetAsync("Visibility", Bool)
	end)
	if Error then
		warn("Could not save")
	end
end)

Remote2.OnServerEvent:Connect(function(Player)
	local Data = DSS:GetDataStore(Player.UserId.."ButtonVisibility")
	local Success, Error = pcall(function()
		Data:GetAsync("Visibility")
	end)
	if Error then
		warn("Could not save")
	else
		print(Success)
		return Success
	end
end)

If you are testing this in studio remember to turn on enable studio access to API services in security settings.

You can also replace the remote events with using modulescript instead if you like.

1 Like

I have tried using the DataStore2 module but I couldn’t get it to work.
I am trying to make a GUI that says “Purchased” or “Purchase”, when the player purchases the item it changes to “Purchased” and when the player leaves and rejoins the game the button is still “Purchased”.
I have tried using the DataStore2 module but I couldn’t get it to work.
local DataStore2 = require(game.ServerScriptService.DataStore2)
local Button = script.Parent
local player = game.Players.LocalPlayer
local dataStore = DataStore2(“test”, player)

function purchase()
Button.Text = “Purchased”
Button.BackgroundColor3 = Color3.new(0, 0, 0)
Button.TextColor3 = Color3.new(0, 0, 0)
Button.BorderSizePixel = 0
Button.AutoButtonColor = false
Button.MouseButton1Click:connect(function()
if Button.Text == “Purchased” then
return
end
end)
end

Button.MouseButton1Click:connect(function()
if Button.Text == “Purchase” then
dataStore:Set(Button.Visible)
purchase()
end
end)

I expected the button to be visible = false when the player rejoins but it is still visible = true.

You’re not actually setting the button’s visibility to false, you’re just setting the data store to false. You need to set the button’s visibility to false.
Button.Visible = false