How can I get a Settings GUI to save?

Hey, how can I make it so it only saves once the player leaves? Instead of it doing it everytime you change it.

--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")
local volume

RE.OnServerEvent:Connect(function(player, volumes)
	volume = volumes
end)

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)

Players.PlayerRemoving:Connect(function(player)
	local key = "Volume_"..player.UserId
	local res = VolumeDS:SetAsync(key, volume)
end)

game:BindToClose(function(player)
	local key = "Volume_"..player.UserId
	local res = VolumeDS:SetAsync(key, volume)
end)

I seem to get this error

--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")
local volume

RE.OnServerEvent:Connect(function(player, volumes)
	volume = volumes
end)

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)

Players.PlayerRemoving:Connect(function(player)
	local key = "Volume_"..player.UserId
	local res = VolumeDS:SetAsync(key, volume)
end)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		local key = "Volume_"..player.UserId
		local res = VolumeDS:SetAsync(key, volume)
	end
end)

Forgot to rewrite “BindToClose()” I just copied it.

1 Like

Alright thanks, it works! Much appreciated! By the way, do I do the same thing for other Settings that I make?

Hey! Why do I get this sometimes?

That happens if you don’t change the setting (the variable named “volume” isn’t changed), this will fix that:

--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")
local volume

RE.OnServerEvent:Connect(function(player, volumes)
	volume = volumes
end)

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)

Players.PlayerRemoving:Connect(function(player)
	if not volume then
		return
	end
	local key = "Volume_"..player.UserId
	local res = VolumeDS:SetAsync(key, volume)
end)

game:BindToClose(function()
	for _, player in pairs(Players:GetPlayers()) do
		if not volume then
			return
		end
		local key = "Volume_"..player.UserId
		local res = VolumeDS:SetAsync(key, volume)
	end
end)
2 Likes

Alright thank you! Now how can I make this work for future settings that I add? Do I do the same stuff that you did? What would I need to change?.. for example

script.Parent.MouseButton1Click:Connect(function()
	if game.Lighting.Brightness == 2 then
		game.Lighting.Brightness = 4
		script.Parent.Text = "Bright"
		
	else
		
		if game.Lighting.Brightness == 4 then
			game.Lighting.Brightness = 0
			script.Parent.Text = "Dark"
			
	else
			
		if game.Lighting.Brightness == 0 then
			game.Lighting.Brightness = 2
			script.Parent.Text = "Normal"
			end
		end
	end
end)

hey sorry for the super late reply but I have a question. What if you want to mute multiple sounds at once?