I am trying to create a datastore for lighting settings in my game, which are adjustable. However, when I join the game, change some of the settings, leave, then rejoin, the settings aren’t saved. I am not getting any errors in the output. Also, the output for the line print(player.Name … " loaded settings:\n" … formattedSettings) appears, but doesn’t have any settings inside. Any help is appreciated.
Here is the server side script (located in Server Script Service:
-- Define the data store key for storing settings
local DATA_STORE_KEY = "LightingSettings"
-- Function to save settings to data store
local function saveSettings(player, brightness, saturation, contrast, shadows, sky)
local dataStore = game:GetService("DataStoreService"):GetDataStore(DATA_STORE_KEY)
local success, errorMessage = pcall(function()
local data = {
Brightness = brightness,
Saturation = saturation,
Contrast = contrast,
Shadows = shadows,
Sky = sky
}
local serializedData = game:GetService("HttpService"):JSONEncode(data)
dataStore:SetAsync(player.UserId, serializedData)
end)
if not success then
warn("Failed to save settings: " .. errorMessage)
end
end
-- Function to load settings from data store
local function loadSettings(player)
local dataStore = game:GetService("DataStoreService"):GetDataStore(DATA_STORE_KEY)
local success, serializedData = pcall(function()
return dataStore:GetAsync(player.UserId)
end)
if success and serializedData then
local data = game:GetService("HttpService"):JSONDecode(serializedData)
return data
else
warn("Failed to load settings")
return nil
end
end
-- Define the RemoteEvents
local saveSettingsEvent = game:GetService("ReplicatedStorage"):WaitForChild("SaveSettingsEvent")
local loadSettingsEvent = game:GetService("ReplicatedStorage"):WaitForChild("LoadSettingsEvent")
-- Handle saving settings
saveSettingsEvent.OnServerEvent:Connect(function(player, brightness, saturation, contrast, shadows, sky)
saveSettings(player, brightness, saturation, contrast, shadows, sky)
end)
-- Handle loading settings
loadSettingsEvent.OnServerEvent:Connect(function(player)
local loadedSettings = loadSettings(player)
if loadedSettings then
local formattedSettings = ""
for key, value in pairs(loadedSettings) do
formattedSettings = formattedSettings .. key .. ": " .. tostring(value) .. "\n"
end
print(player.Name .. " loaded settings:\n" .. formattedSettings)
end
end)
Here is the client side script (located in Starter Player Scripts):
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local saveSettingsEvent = ReplicatedStorage:WaitForChild("SaveSettingsEvent")
local loadSettingsEvent = ReplicatedStorage:WaitForChild("LoadSettingsEvent")
-- Example function to save settings
local function saveSettings(brightness, saturation, contrast, shadows, sky)
saveSettingsEvent:FireServer(brightness, saturation, contrast, shadows, sky)
end
-- Example function to load settings
local function loadSettings()
loadSettingsEvent:FireServer()
end
-- Example usage
saveSettings()
loadSettings()