How to save intValue & is it possible?

Data Stores | Roblox Creator Documentation. Look into that. It shows you how to save data with a specific player.

You’d just save the int to datastores, then when the player joins set the IntValue in the UI to the int saved to datastores.

Edit:

Yep! They’ll need to send the data to the server then use datastores. Security isn’t/shouldn’t be an issue since the client can already control the client copy. Remember to sanity check the remote event!

@co_existance @PersonifiedPizza you can’t use datastores in localscript, so you need remoteevents.

Yeah, I’m just showing him the documentation for the data stores. Was assuming he already knew how to use RemoteEvents (do you?)

1 Like

They asked if it’s still possible to use DataStores because the value is on the client. To me, that sounds rather like they’re unaware of how to get client data in a datastore. The documentation for remote events seems well explained on how to get data from the client to the server. Then you can use DataStore to save it as @co_existance rightfully gave documentation to.

1 Like

would you want to do something like this?

Server:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("YourRemote")
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("MyDataStore") 

remote.OnServerEvent:Connect(function(plr, desiredValue)
        local success, err = pcall(function()
        datastore:SetAsync(plr.UserId, desiredValue) -- arg #1 is the key used to access the saved data in the future
        end)
end)


client:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remote = replicatedStorage:WaitForChild("YourRemote")

yourguibutton.MouseButton1Click:Connect(function()
local int = script.Parent:FindFirstChild("intValue")
remote:FireServer(int.Value)
end)

I don’t know if this will work because I haven’t tested it, but it should.

Couldn’t I have a server script which just takes the intValue inside of StarterGui than saves it to a DataStore?

Where would these two scripts go? Also, I’m looking for a script that gets the intValue from StarterGui, where the int is located, than saves it. Also, this script updates the DataStore every time the player clicks it, lets roblox has a queue so it may not save data that way, PlayerRemoving is a better way to save DataStores.

I’ve written some code that is based off of a script that saves teams, and I’ve TRIED to modify it to save the value, it doesn’t work very well though.

code:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SplitsDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local data

	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-splits")
		print(data) 
	end)
	if success then
		print("Success, load SplitsDataStore")
		print(data)
		if data then
			game.StarterGui.ScreenGui.TextButton.intValue.Value = game.StarterGui.ScreenGui.TextButton.intValue.Value[data]
		else
			game.StarterGui.ScreenGui.TextButton.intValue.Value = "0"
		end
	else
		print(errorMessage)
		game.StarterGui.ScreenGui.TextButton.intValue.Value="0"
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local splits = game.StarterGui.ScreenGui.TextButton.intValue.Value

	local success, errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-splits", splits)
	end)
	if success then
		print("Saved data succesfully")
	else
		print(errorMessage)
	end
end)

So, here there isn’t anything functionally wrong with what you are doing essentially, (with regards to storing the data).

The problem you seem to be encountering is that you are attempting to save data that lies in StarterGUI which won’t reflect the value that currently exists for the player. This is also a bad practice just in general. Instances in StarterGui are simply cloned into the PlayerGui for each client whilst the game is running which is where their unique value will be stored.

Another problem that exists here is that you are also attempting to store a value that only gets updated on the client side of the game, which won’t be known to the server. This results in the IntValue defaulting to 0, because the server doesn’t recognize that the value was updated on the server, only the client knows this.

Is there another way to do it? The reason I have it in StarterGui is because I have a TextButton that needs to display it. Would it work if I put it in ServerScriptService, but wouldn’t it change for everyone? I’m making a game where you click the button, you do a split, than the number goes up by one, and it shows you in a TextButton (the one that you click to do a split), so it will vary for everyone. If I put the value in ServerScriptService, which obviously runs on the server, will it still save for every unique player?

If you want the server to see these changes, you will need to have a unique IntValue for every player that joins the game. For this I recommend creating some kind of system to allow for this.

For creating the Integer saving system you might want to refer to this. In-Experience Leaderboards | Roblox Creator Documentation

And than creating the client → serer update protocol for that value you might want to check this out as well… Need help with using RemoteEvents to change leaderstats - #3 by KingOfHungry

Thank you, I have done that! How can I change when the player clicks that button though, since clicking a button is a client change.

Code:

local Players = game:GetService("Players")
 
local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
 
	local gold = Instance.new("IntValue")
	gold.Name = "Splits"
	gold.Value = 0
	gold.Parent = leaderstats
end
 
-- Connect the "leaderboardSetup()" function to the "PlayerAdded" event
Players.PlayerAdded:Connect(leaderboardSetup)

For that you are going to want some kind of RemoteEvent in ReplicatedStorage that gets fired when you want the value to update. There are many resources related to doing this kind of thing on the forums and the developer hub so i strongly encourage that you attempt to seek this for yourself.

Could I detect when a button is hit (Client) and also update the leaderboard (Server) with a RemoteEvent? I’m not an amazing scripter, so I still am super lost.

When a value in leaderstats is updated, the leaderboard also gets updated.

I would suggest using the datastore2 module instead of the normal datastore, its robust, never acts weirdly, and super easy to use. For your simple case this tutorial works. Its pretty long so skip around to spare your time, the code isnt even that condensed.

You would want to just use a remote event to fire the int value to the server (you could handle this via a local script in startergui), and then save it on the server (in serverscriptservice).

well you could make a leader stat and make the gui stuff sync with the leaderstat and save the leaderstat using datastore

DataStores are server sided, so you can’t. You’ll need to send it to the server.