Hello! How can I make it when you disable a setting, for example, let’s say a Music setting, it save the next time you join so you don’t have to keep changing it. This could include other ones, but I only have a Music one right now. How can I do this? I know I can do it with a Datastore, but I am not really good at those.
When you’re saving a table inside the DataStore:SetAsync() or DataStore:UpdateAsync() function, you should include a table in it where the key is called Settings, then inside the value, you add another table and each element in it save your settings, for example it could be like SettingsGui.MuteSound.Value where Value is a data type data store can save. You get the ide.
The most practical and maybe easiest way to save a GUI (settings, preferences, etc) is to create values for each setting and save each if and once their value changes.
--SERVER SCRIPT--
local DSService = game:GetService("DataStoreService")
local VolumeDS = DSService:GetDataStore("VolumeDS")
local Players = game:GetService("Players")
local Storage = game:GetService("ReplicatedStorage")
local RE = Storage:WaitForChild("RemoteEvent")
Players.PlayerAdded:Connect(function(player)
local key = "Volume_"..player.UserId
local res = VolumeDS:GetAsync(key)
if type(res) == "number" then
RE:FireClient(player, res)
end
end)
RE.OnServerEvent:Connect(function(player, volume)
local key = "Volume_"..player.UserId
local res = VolumeDS:SetAsync(key, volume)
end)
--LOCAL SCRIPT
local Storage = game:GetService("ReplicatedStorage")
local RE = Storage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
if game.SoundService.GameSound.Volume == 0.5 then
game.SoundService.GameSound.Volume = 0
script.Parent.BackgroundColor3 = Color3.new(1, 0.14902, 0.14902)
script.Parent.Text = "Off"
RE:FireServer(game.SoundService.GameSound.Volume)
elseif game.SoundService.GameSound.Volume == 0 then
game.SoundService.GameSound.Volume = 0.5
script.Parent.BackgroundColor3 = Color3.new(0.0980392, 1, 0)
script.Parent.Text = "On"
RE:FireServer(game.SoundService.GameSound.Volume)
end
end)
RE.OnClientEvent:Connect(function(volume)
if volume == 0.5 then
game.SoundService.GameSound.Volume = 0.5
script.Parent.BackgroundColor3 = Color3.new(0.0980392, 1, 0)
script.Parent.Text = "On"
elseif volume == 0 then
game.SoundService.GameSound.Volume = 0
script.Parent.BackgroundColor3 = Color3.new(1, 0.14902, 0.14902)
script.Parent.Text = "Off"
end
end)
You may want to add a debounce to prevent things from being executed too frequently.
I’m not sure how things are organised on your end but you’ll need a RemoteEvent instance named “RemoteEvent” inside the “ReplicatedStorage” folder.
Am I doing my debounce wrong? Once I click, it doesn’t let me click again.
--LOCAL SCRIPT
local Storage = game:GetService("ReplicatedStorage")
local RE = Storage:WaitForChild("RemoteEvent")
local Player = game.Players.LocalPlayer
local db = false
script.Parent.MouseButton1Click:Connect(function()
if db == false then
db = true
if game.SoundService.GameSound.Volume == 0.5 then
game.SoundService.GameSound.Volume = 0
script.Parent.BackgroundColor3 = Color3.new(1, 0.14902, 0.14902)
script.Parent.Text = "Off"
RE:FireServer(game.SoundService.GameSound.Volume)
elseif game.SoundService.GameSound.Volume == 0 then
game.SoundService.GameSound.Volume = 0.5
script.Parent.BackgroundColor3 = Color3.new(0.0980392, 1, 0)
script.Parent.Text = "On"
RE:FireServer(game.SoundService.GameSound.Volume)
end
end
end)
RE.OnClientEvent:Connect(function(volume)
if volume == 0.5 then
game.SoundService.GameSound.Volume = 0.5
script.Parent.BackgroundColor3 = Color3.new(0.0980392, 1, 0)
script.Parent.Text = "On"
elseif volume == 0 then
game.SoundService.GameSound.Volume = 0
script.Parent.BackgroundColor3 = Color3.new(1, 0.14902, 0.14902)
script.Parent.Text = "Off"
wait(1)
db = false
end
end)