Hello!
I am trying to make a settings page for my game but am having trouble saving it. This setting page would just store bool, number and string values for each of the settings through a table/dictionary.
My issue is that it isn’t saving. I have a ‘test button’ that when you click on it it changes the value of one of the settings, but when I rejoin, it doesn’t save.
Solutions I have tried are RemoteEvents, rewriting code, Developer Hub and the DevForum, all to no avail.
I will really appreciate it if any of you guys can help.
ServerScript (in SSS)
local settingsTable = require(game.ReplicatedStorage.SettingsTable)
local notificationModule = require(game.ReplicatedStorage.NotificationModule)
local plr = game.Players.LocalPlayer
local settingsTableSuccess = {
BackgroundColor = Color3.fromRGB(0,0,0);
TextColor = Color3.fromRGB(255,255,255);
Text = "Data was saved Succesfully!";
Duration = 10;
Player = plr;
}
local settingsTableHasntSaved = {
BackgroundColor = Color3.fromRGB(0,0,0);
TextColor = Color3.fromRGB(255,255,255);
Text = "Data hasn't been saved!";
Duration = 10;
Player = plr;
}
local settingsTableNoData = {
BackgroundColor = Color3.fromRGB(0,0,0);
TextColor = Color3.fromRGB(255,255,255);
Text = "This player has no data!";
Duration = 10;
Player = plr;
}
-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("SettingsDataStore") -- This can be changed to whatever you want
local function saveData(player) -- The functions that saves data
local tableToSave = {
settingsTable;
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
notificationModule:CreatePlayerNotification(settingsTableSuccess)
else -- Else if the save failed
notificationModule:CreatePlayerNotification(settingsTableHasntSaved)
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
local data -- We will define the data here so we can use it later, this data is the table we saved
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
end)
if success then -- If there were no errors and player loaded the data
settingsTable = data -- Set the money to the first value of the table (data)
else -- The player didn't load in the data, and probably is a new player
notificationModule:CreatePlayerNotification(settingsTableNoData) -- The default will be set to 0
end
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
notificationModule:CreatePlayerNotification(settingsTableSuccess)
else
notificationModule:CreatePlayerNotification(settingsTableHasntSaved)
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
notificationModule:CreatePlayerNotification(settingsTableSuccess)
else
notificationModule:CreatePlayerNotification(settingsTableHasntSaved)
end
end
end)
ModuleScript (in RS)
local settingsTable = {
DarkMode = false,
Brightness = 1,
MuteAllAudio = false,
DebugMode = false,
CreatorCode = "",
}
return settingsTable
TestScript (in SG)
local tButton = script.Parent.Container.DarkMode.testButton
local settingsTable = require(game.ReplicatedStorage.SettingsTable)
print(settingsTable["DarkMode"])
tButton.MouseButton1Click:Connect(function()
settingsTable["DarkMode"] = true
wait(2)
print(settingsTable["DarkMode"])
end)
Thanks in advance,
cursecode