How do I make a data store for a local player?

I have this shadow datastore, it works, the shadows are saved when exiting and entering the data is loaded, but when you change the shadow, it is changed for all players, and I only want it to be changed for only one.

   local ReplicatedStorage = game:GetService("ReplicatedStorage")
    local DataStoreService = game:GetService("DataStoreService")
    local Players = game:GetService("Players")
    local Lighting = game:GetService("Lighting")

    local graficos = ReplicatedStorage:WaitForChild("Graficos")
    local MyDataStore = DataStoreService:GetDataStore("MyDataStore")

    local shadowSettings = {
    	Bajo = function(player: Player)
    		
    		Lighting.GlobalShadows = false
    		Lighting.ShadowSoftness = 1
    		for _, item in ipairs(workspace:GetDescendants()) do
    			if item:IsA("PointLight") then 
    				item.Shadows = false
    				item.Brightness = 0.35
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Bajo").Frame.Visible = true
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Alto").Frame.Visible = false
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Medio").Frame.Visible = false
    			end
    		end
    		
    	end,
    	Medio = function(player: Player)
    		
    		Lighting.GlobalShadows = true
    		Lighting.ShadowSoftness = 0.5
    		for _, item in ipairs(workspace:GetDescendants()) do
    			if item:IsA("PointLight") then 
    				item.Shadows = false
    				item.Brightness = 0.35
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Bajo").Frame.Visible = false
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Alto").Frame.Visible = false
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Medio").Frame.Visible = true
    			end
    		end
    		
    	end,
    	Alto = function(player:Player)
    		
    		Lighting.GlobalShadows = true
    		Lighting.ShadowSoftness = 0.1
    		for _, item in ipairs(workspace:GetDescendants()) do
    			if item:IsA("PointLight") then 
    				item.Shadows = true
    				item.Brightness = 0.75
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Bajo").Frame.Visible = false
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Alto").Frame.Visible = true
    				player:WaitForChild("PlayerGui"):WaitForChild("MenuDeMapa"):WaitForChild("Graficos"):WaitForChild("Sombras"):WaitForChild("Medio").Frame.Visible = false
    			end
    		end
    		
    	end,
    }

    game.Players.PlayerAdded:Connect(function(player: Player)
    	local graficosFol = Instance.new("Folder")
    	graficosFol.Name = "Graficos"
    	graficosFol.Parent = player

    	local graphicsBools = {
    		{type = "StringValue", name = "Shadow"},
    		-- Add more values for your graphics folder!
    		--{tipo = "String Value", nombre = "Nombre de la configuración"},
    	}

    	for _, value in pairs(graphicsBools) do
    		local val = Instance.new(value.type)
    		val.Name = value.name
    		val.Parent = graficosFol
    	end

    	local data
    	local succes: boolean, errormessage: string = pcall(function()
    		data = MyDataStore:GetAsync(player.UserId.."Value")
    	end)
    	
    	if (succes) then
    		
    		-- If there is data already
    		if (data) then
    			-- You can set more values if you'd like
    			player:WaitForChild("Graficos"):WaitForChild("Shadow").Value = data.shadow
    			shadowSettings[data.shadow](player)
    		else
    			-- No data, give them default data to start off
    			player:WaitForChild("Graficos"):WaitForChild("Shadow").Value = "Medio"
    			shadowSettings.Medio(player)
    		end
    		
    	else
    		print("Se ha producido un error al guardar datos")
    		warn(errormessage)
    	end
    end)

    graficos.ChangeSetting.OnServerEvent:Connect(function(player: Player, setting: string, value: string)
    	
    	if (not player.Graficos:FindFirstChild(setting)) then return end
    	if (not shadowSettings[value]) then return end
    	
    	player.Graficos[setting].Value = value
    	shadowSettings[value](player)
    end)

    game.Players.PlayerRemoving:Connect(function(player: Player)
    	local values = {
    		-- Get all the values!
    		shadow = player.Graficos.Shadow.Value
    	}

    	local success: boolean, errormessage: string = pcall(function()
    		MyDataStore:SetAsync(player.UserId .. "Value", values)
    	end)

    	if (success) then
    		print("Se ha guardado correctamente")
    	else
    		print("hubo un error al guardar")
    		warn(errormessage)
    	end
    end)
1 Like

You would need to use remote events to send the data to the server.

Edit: You would use keys to only save for that specific player. The key could be their UserId.

Yes I used remote events to send the signal

Are you changing the shadows on a server script?

Yes, because if not, the shadows are not saved

Changing it on a server script will change for all players you could use FireClient to change it for the one player.

The datastore is in ServerScriptService, this is my script that sends the signal

local main = script.Parent

local bajo = main.Bajo

local Medio = main.Medio

local Alto = main.Alto

local graficos = game:GetService("ReplicatedStorage"):WaitForChild("Graficos")

--[[ We have these settings for Shadow:

local shadowSettings = {

"Bajo",

"Medio",

"Alto"

}]]

-- Set Shadow to Bajo

bajo.MouseButton1Down:Connect(function()

graficos.ChangeSetting:FireServer("Shadow", "Bajo")

end)

-- Set shadow to Medio

Medio.MouseButton1Down:Connect(function()

graficos.ChangeSetting:FireServer("Shadow", "Medio")

end)

-- Set shadow to Alto

Alto.MouseButton1Down:Connect(function()

graficos.ChangeSetting:FireServer("Shadow", "Alto")

end)

You can make a variable of the new shadow in a LocalScript and then use a RemoteEvent to send it to the Server Script.

I had an idea, a local script detects the value “Low”, “Medium”, “High”, and it depends on the Value, but how do I do it?

Edit: The value is a StringValue

For example make a variable of the shadown in a local script, a variable of a remote event and write RemoteEvent:FireServer(Shadow) --Change shadow with your variable's name and RemoteEvent with the name of your RemoteEvent variable

In a Server Script you can make the same variable of the RemoteEvent:

RemoteEvent.OnServerEvent:Connect(function(Shadow) --The parameter of the function must be the same of the one made in the LocalScript

end)

I did it like this

local RemoteEvent = game.ReplicatedStorage:WaitForChild("Graficos"):WaitForChild("ChangeSetting")
local Lighting = game:GetService("Lighting")

RemoteEvent.OnServerEvent:Connect(function(Bajo) --The parameter of the function must be the same of the one made in the LocalScript		
	Lighting.GlobalShadows = false
	Lighting.ShadowSoftness = 1
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = false
			item.Brightness = 0.35
		end
	end
end)

RemoteEvent.OnServerEvent:Connect(function(Medio) --The parameter of the function must be the same of the one made in the LocalScript		
	Lighting.GlobalShadows = true
	Lighting.ShadowSoftness = 0.5
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = false
			item.Brightness = 0.35
		end
	end
end)

RemoteEvent.OnServerEvent:Connect(function(Alto) --The parameter of the function must be the same of the one made in the LocalScript		
	Lighting.GlobalShadows = true
	Lighting.ShadowSoftness = 0.1
	for _, item in ipairs(workspace:GetDescendants()) do
		if item:IsA("PointLight") then 
			item.Shadows = true
			item.Brightness = 0.75
		end
	end
end)

You could use tables for this, Simple Example:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local Datos = {}
local PlayerAdded = function(Plr)
	local Sucess,Error = pcall(function()
		Data = DataStore:GetAsync(Plr.UserId)
	end)
	if Data ~= nil then
		Datos[Plr] = Data
	else
		Datos[Plr] = 0
	end
end
local PlayerRemoving = function(Plr)
	local Sucess,Error = pcall(function()
		DataStoreService:GetAsync(Plr.UserId,Datos[Plr])
	end)
end
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

this way is more efficient when you look storing data for each player

Ah, not really, it is a “Server ScriptService” script But I already solved it