Need help with saving local data

Hello, fella devs,

I am trying to make a game setting and I need help with it. How can I save a player setting data for client and not for the server. Which mean, the setting is local. I trying to figured out how and I just realize that I am a stupid man. That’s all. Hope you can help :>.

1 Like

You can use a datastore, and create a variable that enables it.

I agree with @ZacharyZaxorDDD

Try using remote events, then use datastores.

(From what I know, datastores are server - only)

1 Like

i mean, how can i save it from this?image

1 Like

No, you just need to save the variable that enables or disable it like local variable = false.

oh ok. I’ll try. I hope it work

1 Like

I think it going to work but. I have this error. Here is the script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataService = game:GetService("DataStoreService")

local events = ReplicatedStorage:WaitForChild("Events")
local functions = ReplicatedStorage:WaitForChild("Functions")
local optionData = DataService:GetDataStore("optionData")

local loadOptionSettings = functions:WaitForChild("loadOptionSettings")

local togglePopup = events:WaitForChild("togglePopup")
local sendContrast = events:WaitForChild("sendContrastSetting")
local sendShadow = events:WaitForChild("sendShadowSetting")
local sendFog = events:WaitForChild("sendFogSetting")

local optionSettings = {}

local function saveData(player)
	local success, err = pcall(function()
		optionData:SetAsync(player.UserId, optionSettings)
	end)
	
	if success then
		print("Option settings data saved for", player)
	else
		warn(err)
	end
end

local function loadData(player)
	local success, data
	local tries = 0
	repeat
		tries += 1
		success, data = pcall(function()
			return optionData:GetAsync(player.UserId)
		end)
		if not success then wait(1) end
	until tries == 3 or success
	
	if success then
		if data then
			if not data["HighContrast"] then
				data["HighContrast"] = false
				data["Shadow"] = true
				data["Fog"] = false
			end
			
			optionSettings[player.UserId] = data
		else
			optionSettings[player.UserId] = {
				["HighContrast"] = false,
				["Shadow"] = true,
				["Fog"] = false
			}
		end
		togglePopup:FireClient(player, "Error: Failed to load your option settings data", false)
		warn("Failed to load option setting data for", player)
	end
end

local function giveOptionSettings(player)
	if optionSettings[player.UserId] then
		return optionSettings[player.UserId]
	else
		return nil
	end
end
loadOptionSettings.OnServerInvoke = giveOptionSettings

sendContrast.OnServerEvent:Connect(function(player, enabled)
	optionSettings[player.UserId]["HighContrast"] = enabled
end)

sendShadow.OnServerEvent:Connect(function(player, enabled)
	optionSettings[player.UserId]["Shadow"] = enabled
end)

sendFog.OnServerEvent:Connect(function(player, enabled)
	optionSettings[player.UserId]["Fog"] = enabled
end)

Players.PlayerAdded:Connect(function(player)
	loadData(player)
end)

Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

game:BindToClose(function()
	for index, player in pairs(Players:GetPlayers()) do
		saveData(player)
	end
end)

Im a little bit late on this, but what I did to fix this issue was added a bunch of attributes to the player instance using a server script (updated with remote events containing the parameters). Then the data store could read the attributes and save them.

1 Like