Hello, so I have a script that detects the BoolValues and what value they are but it doesn’t detect when they’re changed.
I’m working a script that saves what settings you have and can’t get the values to save when they’re changed.
Video:
Script:
local settingsFolder = game.ReplicatedStorage.Settings
local function printBoolValues()
print("Printing current values of BoolValues:")
for _, child in ipairs(settingsFolder:GetChildren()) do
if child:IsA("BoolValue") then
local valueString = child.Value and "true" or "false"
print(child.Name .. ": " .. valueString)
end
end
end
local function onBoolValueChanged(boolValue)
boolValue.Changed:Connect(function(newValue)
local valueString = newValue and "true" or "false"
print(boolValue.Name .. " value changed to: " .. valueString)
end)
end
printBoolValues()
for _, child in ipairs(settingsFolder:GetChildren()) do
if child:IsA("BoolValue") then
onBoolValueChanged(child)
end
end
while true do
wait(10)
printBoolValues()
end
I have not personally tested this code, but I have edited some of it and left explanations on why I did it.
local settingsFolder = game.ReplicatedStorage.Settings
local function printBoolValues()
print("Printing current values of BoolValues:")
for _, child in settingsFolder:GetChildren() do -- ipairs() is not required
if child:IsA("BoolValue") then
local valueString = child.Value and true or false -- BoolValues cannot be "true" or "false", they are true or false exactly
print(child.Name .. ": " .. valueString)
end
end
end
local function onBoolValueChanged(boolValue)
boolValue.Changed:Connect(function(newValue)
local valueString = newValue and true or false -- same as before
print(boolValue.Name .. " value changed to: " .. valueString)
end)
end
printBoolValues()
for _, child in settingsFolder:GetChildren() do -- ipairs() is not required
if child:IsA("BoolValue") then
onBoolValueChanged(child)
end
end
while true do
wait(10)
printBoolValues()
end
Heres the actual data store script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local settingsDataStore = DataStoreService:GetDataStore("PlayerSettings")
local function savePlayerSettings(player)
local playerId = player.UserId
local settingsFolder = game.ReplicatedStorage:FindFirstChild("Settings")
if not settingsFolder then
warn("Settings folder not found in ReplicatedStorage.")
return
end
local data = {}
for _, boolValue in pairs(settingsFolder:GetChildren()) do
if boolValue:IsA("BoolValue") then
data[boolValue.Name] = boolValue.Value
end
end
local success, errorMessage = pcall(function()
settingsDataStore:SetAsync(playerId, data)
end)
if success then
print("Player data saved successfully for player "..playerId)
else
warn("Failed to save player settings for player "..playerId..": "..tostring(errorMessage))
end
end
local function loadPlayerSettings(player)
local playerId = player.UserId
local settingsFolder = game.ReplicatedStorage:FindFirstChild("Settings")
if not settingsFolder then
warn("Settings folder not found in ReplicatedStorage.")
return
end
local success, data = pcall(function()
return settingsDataStore:GetAsync(playerId)
end)
if success and data then
for name, value in pairs(data) do
local boolValue = settingsFolder:FindFirstChild(name)
if boolValue and boolValue:IsA("BoolValue") then
boolValue.Value = value
end
end
print("Player data loaded successfully for player "..playerId)
elseif not success then
warn("Failed to load player settings for player "..playerId..": "..tostring(data))
end
end
local function updateDataStore(boolValue)
local player = game.Players:GetPlayerFromCharacter(boolValue.Parent)
if not player then
return
end
local playerId = player.UserId
local data = {}
data[boolValue.Name] = boolValue.Value
local success, errorMessage = pcall(function()
settingsDataStore:SetAsync(playerId, data)
end)
if success then
print("Player data updated successfully for player "..playerId)
else
warn("Failed to update player settings for player "..playerId..": "..tostring(errorMessage))
end
end
for _, boolValue in ipairs(game.ReplicatedStorage.Settings:GetDescendants()) do
if boolValue:IsA("BoolValue") then
boolValue.Changed:Connect(function()
updateDataStore(boolValue)
end)
end
end
Players.PlayerAdded:Connect(loadPlayerSettings)
Players.PlayerRemoving:Connect(savePlayerSettings)
It saves and loads according the my prints but it somehow isn’t detecting when the boolvalue’s value is changed so when rejoining the game my settings just go back to default.
It would be more fitting to save all of the BoolValues when the user is leaving, using game.Players.PlayerRemoved(Player)
.
The change of bool in BoolValues may have only happened on the client, meaning the BoolValues in the server are unaffected. You can use either RemoteEvents or RemoteFunctions to send data from the client to the server.