How would I make a datastore on dark or light mode?

I want to create a datastore using datastore2 that stores whether the user has chosen dark mode or light mode on my UI. I have already made the dark/light mode functionality, just looking to save it for their next visit.

I have never used datastores and have little knowledge on them.

1 Like

Well you will be glad to know, Roblox has created a tutorial on how to do datastores.
Tutorial

Essentially a data store is just a save key and the data that accompanies it, you can save entire tables to singular values which is great. If you need any help understanding the tutorial, feel free to ask :slightly_smiling_face:

2 Likes

Thanks for the link, I’ve had a scan through it, how would I read a datastore from a localscript? My dark mode script is ran from a localscript. Also, I know you can’t save it from a localscript, so how would I save it?

remote events you can do it so

Make a remote event in replicated Storage then in the local script (where you change dark mode or whatever) just add this line:

--rest of code
local RemoteEvent = --yourRemoteEvent
RemoteEvent:Fire("LIGHT MODE")--pass argument here such as "LIGHT MODE" or "DARK MODE"

Back in Another script (datastore script is recommended) add these lines

local RemoteEvent = --yourRemoteEvent
RemoteEvent.OnServerEvent:Connect(function(player,save)
        Datastore:SetAsync(player.UserId,save)--save here
end)

For getting it’s also quite similar

local Value = Datastore:GetAsync(KEY)  --Your key or player.UserId
if Value == "LIGHT MODE" then
--Code to set it
else
--Code
1 Like

Mhh, you can do this easely, just make sure to follow a tutorial on how to create a datastore and then
you probably created a value or something like this in the plr to know if he shoosed light or dark.
If you don’t, you can simply make an intValue in the plr called “Mode” and when the plr joins for the first time this value is equal to 0 and if he shoose light you changed this value to 1 and if he shooses dark you change it to 2 and then you only have to save it with a key in the datastore : something like :

local DSS = game:GetService("DataStoreService"):GetDataStore("DataStore1") -- Name it whatever you like.

game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEvent").OnServerEvent:Connect(function(player, mode)
   local success, error = pcall(function()
      DSS:SetAsync("Mode-"..player.UserId, value.Value) 
   end)
end

You can put the line i wrote upper in a server script wich you can run with a remote event fired by the client (local script).

Local script runs on the player’s client so you can’t save it from here, so put a remote event in the replicatedStorage, name it whatever you like and in your local script, when you want to save just do:

local event = game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEvent")

event:FireServer(send your value here)

This is a way to do it, if you want to do this path and if you have questions, contact me :slight_smile:
Hope it will help you

I would save the background color of the UI in a folder inside the player, then set it to that when they join again.

Using some help from ^^^

It’s the whole UI, the text, the music system everything is changed just for darkmode and lightmode.

1 Like

image What do I put as the value?

the value you actually want to set

Thats the NumberValue or IntValue you have made inside the player.

Here is a quick explanation on how other users comments work in simpler terms.
Basically, you have a Value inside the player that is either 1, or 2. 1 is Light, 2 is Dark. You have a button that changes the value when you click it by using a RemoteEvent or RemoteFunction so that it updates server side. Then, you simply save the value chosen.

So this should be the server script

local value = game.Players.LocalPlayer.Lighting
local DSS = game:GetService("DataStoreService"):GetDataStore("LightingStore")
game:GetService("ReplicatedStorage"):WaitForChild("LightingMode").OnServerEvent:Connect(function(player, mode)
	   local success, error = pcall(function()
		
      DSS:SetAsync("Mode-"..player.UserId, value.Value) 
   end)
end)

and this in the localscript?

event:FireServer(2) -- Dark Mode = 2

In the local script you want

event:FireServer(2, LocalPlayer)

You can’t use :

game.Players.LocalPlayer

Because it works only for a local script.

As I explained it upper, a local script run on the player’s device, so you can find the player of the device.
But the server is a roblox computer wich run for all players.

Clients (Local scripts) can communicate with the server using remote event.

You can fire the server from a local script.

When the server is fire it will get informations from the player who called the server.

So when you fire the server, the player is automaticly sent to the server so on your server script you can get the player as an argument in the brackets like this :

local event = game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEvent")

-- And then you send a function when a player call / fire the server. So the function will run when a player fire the server

event.OnServerEvent:Connect(function(player)

-- So here, you have the player.

I am reall confused lol, if you’re available would you mind checking if I have done it right in team create?

What he’s saying is that you cannot send LocalPlayer over to the server as an argument in a RemoteEvent. When you recieve the RemoteEvent, the arguments are player, and whatever else you have sent. So, no need to send the local player.

I understand that part now, but I’m overall confused about the whole thing.

Then we need the value, and here, we will use arguments !

So in your local script, you change the gui to dark or to light but you can’t change the player’s value on the local script becaue it will not be considerated by the server.

If a value is changed by a client (by a local script) it will not be considerated by the server.

If you want an exemple, do you see games, where there are walls between maps.

Some players can pass through if they buy the map but other can’t.

It because the wall exists on the client but not on the server.

So it’s the same here, if you change the value of lighting it will not affect the server, so just seperate the result doing something like this.
You have your Gui, and i think there is two button, one for light and one for dark, so if light is pressed, fire the server and send as an argument a string like “DARK” or “LIGHT” to the server.

So your local script looks like this :

local event = game.ReplicatedStorage:WaitForChild("YourRemoteEvent")

darkButton.MouseButton1Click:Connect(function()
   event:FireServer("DARK") -- it will send the player as argument 1 and the side (Light or dark) as a second argument.
end

lightButton.MouseButton1Click:Connect(function()
   event:FireServer("LIGHT")
end
-- Here you can add you lines wich change the text of the UI ect...

So now, you have to update the server and to save it, you can do it like this :

local DSS = game:GetService("DataStoreService"):GetDataStore("DataStore1") -- Name it whatever you like
local event = game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEvent")
-- Here we will create the key of the save, you need a unique key for each player so we will use his UserId so: 
local key = "Side-"..plr.UserId -- This will concatinate the string ("Side-" with the player.UserId, it will gives something like this, "Side-2452865896"

-- Now make the function run when the server is fired --

event.OnServerEvent:Connect(function(player, side) -- So here you get as arguments the player and the side, side will be equal to "DARK" if the player choose Dark or "LIGHT" if he shose light.

-- Now you can change the value and save it --

-- We have to find the player value doing : 
local value = player.Lightning

-- Now let's change the value of the lightning on the server, so we have to do:

if side == "DARK" then
   value.Value = 1
elseif side == "LIGHT" then
   value.Value = 2
end

-- Now you can save it : 

-- We'll add this line to avoid the script to crash if it faces an error: 

local success, errormessage = pcall(function() -- and inside you can save the value in normal DSS or it is a bit different for DSS2 
   DSS:SetAsync(key, value.Value) -- Here we will save behind the key we created before the value so 1 for the DARK and 2 for the light. Now when the player joins the game, you have to give back the value of Lightning and if it equal to 0 you make the player choose and if it equals to 1 you change the UI to dark mode and if it equals to 2 you change it to light mode.
end)

If you have question ask them, and i can come help you on a sharred place!

You can’t do data stores on the client side just so your aware, this will cause an error.

Personally, I would use another method if the value won’t be persistent to reduce the need to constantly set data and reduces the chances of having an error. I’m writing an example file - I’ll also include my recent replication security practice which you might want to follow along with. Comments and explanations will be found inside this file :smile:

Note: It will also include a relatively advanced method of client-server replication, definitely worth looking at as it can help with making your games more secure.

Here is a line by line explanation of a reliable method I use, hopefully this should cover all your issues:
datastores.rbxl (25.3 KB)

If you search up ‘server’ and ‘client’ you’ll find the two scripts. Inside you’ll have everything from assumptions I made whilst coding it, to justifications, useful links to explain everything, line by line commenting and simple explanations. Put way to much effort in to this, going to be honest :sweat:

As Maui would say: “You’re Welcome!” :smile: