Sorry for the long wait. I was doing something else, but I made something that should work. I quickly ran through everything (3 minutes), so you should double-check the code to see if there are any data-saving issues.
Server:
--!strict
local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local RS = game:GetService("ReplicatedStorage")
local Ds = DSS:GetDataStore("PlayerGraphicsSettings")
local defaultSettings = {
Atmosphere = true,
Bloom = true,
Blur = true,
Diffuse = true,
Reflections = true,
Shadows = true,
Sunrays = true,
WaterWaveSize = 0.15,
WaterWaveSpeed = 10,
}
local CachedData = {}
local Transmitter = RS.RemoteEvent --Example remote event location
local function GetandSetData(Player:Player, Saving:boolean, Info:any?)
local Tries = 0
local Max = 3
local Sucess, Data = nil
local ID = `{Player.UserId}`
repeat
Tries += 1
Sucess, Data = pcall(Saving and Ds.SetAsync or Ds.GetAsync, Ds, ID, Info)
if not Sucess and Data then warn(Data) end
task.wait(1)
until Sucess or Tries == Max
return if Saving then nil else Data
end
local function OnJoin(Player:Player)
local Data = GetandSetData(Player, false, nil)
Data = Data or defaultSettings
CachedData[Player.UserId] = Data
Transmitter:FireClient(Player, Data)
end
local function OnLeave(Player:Player)
local PlayerData = CachedData[Player.UserId]
if not PlayerData then return end
GetandSetData(Player, true, PlayerData)
PlayerData = nil
end
local function SaveNewData(Player:Player, NewData:any)
if not Player or not NewData or typeof(NewData) ~= "table" then return end
CachedData[Player.UserId] = NewData
end
for _, Player in Players:GetPlayers() do
OnJoin(Player)
end
Transmitter.OnServerEvent:Connect(SaveNewData)
Players.PlayerAdded:Connect(OnJoin)
Players.PlayerRemoving:Connect(OnLeave)
Client:
local RS = game:GetService("ReplicatedStorage")
local Remote = RS:WaitForChild("RemoteEvent")
Remote.OnClientEvent:Connect(function(Settings: any)
--Do something with the settings here...
end)
--Mock example of the client changing some settings
repeat
task.wait(10)
Remote:FireServer({
Atmosphere = true,
Bloom = true,
Blur = true,
Diffuse = true,
Reflections = true,
Shadows = true,
Sunrays = true,
WaterWaveSize = 0.15,
WaterWaveSpeed = math.random(1,10),
})
until false
If you have questions, please ask