Saving the Data of Text Buttons

Recently I’ve posted about a problem I’ve had with ‘DataStore’ stuff. The problem was that the script that was supposed to save the current value of the ‘StringValue’ connected to the ‘TextButton’ and when you rejoin the game, the ‘StringValue’ was supposed to have the value that was set in the last time you joined the game. However that didn’t go really well, tried so many solutions for 1 hour and still did not get it to work properly. I want to forget that script now and if possible, work on a completely different script. Though I’m really bad at scripting in general so I need your help too with this!

I threw away some buttons in a brand new place for testing purposes before putting them into my original game. As I’ve somewhat explained earlier, my goal is to switch between two buttons (as seen below). When you click one of them, the other’s ‘Visible’ property would be set to ‘false’ and the opposite would happen for the other button. There also should be a ‘StringValue’ so whenever you click on one of the buttons, the value would change to what the Button’s Text would say.

Capture

This is the current layout of my explorer tab. You click on ‘TextButton’, it would switch to ‘TextButtonLol’ and same would happen with ‘TextButtonLol’. The ‘ButtonOptions’ script handles this. The code below shows what that script contains:

local button1 = script.Parent.TextButton
local button2 = script.Parent.TextButtonLol

local CurrentButtonValue = game.Players.LocalPlayer.SettingsValues.CurrentButtonValue

button1.MouseButton1Click:Connect(function()
	
	if CurrentButtonValue.Value == "L" then
		
		CurrentButtonValue.Value = "Bozo"
		button1.Visible = false
		button2.Visible = true
		
	end
	
end)

button2.MouseButton1Click:Connect(function()

	if CurrentButtonValue.Value == "Bozo" then

		CurrentButtonValue.Value = "L"
		button1.Visible = true
		button2.Visible = false

	end

end)

I did not add the ‘StringValue’ yet into my explorer because I’m not too sure if I would have to add it with the script that would save the data (MenuSettings) like how I’ve done with the script that failed me basically at this point or could I just add the ‘StringValue’ now and just change the actual value of it in-game.

If you can help me with this I would be really pleased because it looks like I’m gonna need the ‘DataStore’ stuff a lot to complete my game and it would be good to learn the best ways possible.

Thank you!

DataStores can save any string up to a certain length, so you can just save your text to it directly. What is your end goal specifically? The code you posted seems like its a test for something else you want to do.

Sorry for the late reply! This is won’t be really a different script that I’m gonna use in my main game, only the buttons will be different. I just made this place so I could work on this script a lot cleaner because my main game is a bit messy. My goal is to save the last status of the buttons that was set in the last time you joined the game. For example:

You clicked on the ‘TextButton’ in the game so it changed into ‘TextButtonLol’. When you leave it like that and rejoin the game, ‘TextButtonLol’ would show up instead of the default one which is ‘TextButton’.

You could also take how FOV works in first person shooter games as an example. Let’s say the default FOV is 60, you change that to 90 in-game and later on when you rejoin that game, the FOV would’ve stayed at 90.

If you’re using this for settings you can put all your options in a table and use a remote function to request this settings table from a central script. To save and load a table (as opposed to a string) from a data store it needs to be serialized, which there is a built in method to do: JSONEncode