How do i save BoolValue in datastore?

So i has a BoolValue, and if it value changes, it is saving and changing for EVERYONE, ever if they rejoin.

What solutions have you tried so far? A lot, but everyone is showing way to create BoolValue in player, then save it in leaderboards and blablabla. As you guess i has different situation, where i need to get ONE boolvalue that keeps in RS for EVERYONE that saving its value as changed, and changing for EVERYONE, every single player. ever if he rejoined.

note: i has a lot of boolvalues, so i need way to save every of them (for example creating script that i put in every single boolvalue)

Tp achieve this, you can use a combination of a RemoteEvent and a ModuleScript that handles the saving and changing of the BoolValue for all players.

First, create a ModuleScript by right-clicking on your Workspace or ServerScriptService and selecting Insert Object > ModuleScript. Name this module “BoolValueHandler”.

Inside the BoolValueHandler module, you can define functions for saving and changing the BoolValue. Here’s an example implementation:

local BoolValueHandler = {}

function BoolValueHandler:SaveBoolValue(boolValue)
    -- Save the current value of the BoolValue to a datastore or wherever you want to store it
end

function BoolValueHandler:ChangeBoolValue(boolValue, newValue)
    -- Update the value of the BoolValue
    boolValue.Value = newValue

    -- Call the SaveBoolValue function to save the new value
    BoolValueHandler:SaveBoolValue(boolValue)

    -- Trigger a RemoteEvent to notify all players of the new value
    game:GetService("ReplicatedStorage").BoolValueChanged:FireAllClients(boolValue.Name, boolValue.Value)
end

return BoolValueHandler

Now, create a RemoteEvent by right-clicking on your ReplicatedStorage and selecting Insert Object > RemoteEvent. Name this RemoteEvent “BoolValueChanged”.

Next, in a ServerScript (e.g., placed in ServerScriptService), you can handle the RemoteEvent and update the BoolValues for all players. Here’s an example implementation:

local BoolValueHandler = require(game:GetService("ServerScriptService").BoolValueHandler)

game:GetService("ReplicatedStorage").BoolValueChanged.OnServerEvent:Connect(function(player, boolValueName, newValue)
    local boolValue = -- Find the BoolValue in your game based on the boolValueName

    -- Call the function from the BoolValueHandler module to change the BoolValue
    BoolValueHandler:ChangeBoolValue(boolValue, newValue)
end)

Finally, in each BoolValue that you want to save and change for everyone, you can add a Script to handle the value change and call the RemoteEvent to update all players. Here’s an example implementation:

local boolValue = script.Parent -- Assuming the script is placed inside the BoolValue

local BoolValueHandler = require(game:GetService("ServerScriptService").BoolValueHandler)

boolValue.Changed:Connect(function(newValue)
    -- Call the function from the BoolValueHandler module to change the BoolValue
    BoolValueHandler:ChangeBoolValue(boolValue, newValue)
end)

With this setup, whenever a BoolValue changes, it will trigger the BoolValueChanged RemoteEvent, which will then be handled on the server to update the BoolValues for all players. The BoolValueHandler module takes care of saving the new values and updating them accordingly.

1 Like

[quote=“RoDeveloper, post:2, topic:2570269, username:tutab9”]

local BoolValueHandler = require(game:GetService("ServerScriptService").BoolValueHandler)

game:GetService("ReplicatedStorage").BoolValueChanged.OnServerEvent:Connect(function(player, boolValueName, newValue)
    local boolValue = -- Find the BoolValue in your game based on the boolValueName

    -- Call the function from the BoolValueHandler module to change the BoolValue
    BoolValueHandler:ChangeBoolValue(boolValue, newValue)
end)

what do i did wrong it not works