Is there a way to datastore a bool variable

So I am making a system that checks if you have done something for example a quest. When you do it it will make for example a Boolean that’s named (active) false and then when you leave and join back it saves the Boolean to false because you already did it. If anybody knows how to do this please tell me ASAP thank you for your time and have a nice day
Sincerely, DevBattery

ok here’s an easy example
player: does quest.

player.whereyoursavingthedata.Quest1Done.Value = true

player want’s to do quest again, but can’t.

(connectafunctionorsomethinghere)
if player.whereyoursavingthedata.Value == false then
player.whereyoursavingthedata.Quest1Done.Value = true
--do quest
end
2 Likes

That’s a question i also want to know because i want to make achievements in my game and i was thinking of using that too, i think it does work.

1 Like

Thanks for the info but i primarily just want to know what would be the script or like how would you write the script down. Thanks for the info nonetheless.

If you have the data store, it would look something like this ~

(in a server side script, server script service)
(ensure that you have a remote event to hook it to)

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("nameHere")

local remote = [the event variable here]
local getCompleted = [variable for an event to check if its completed]

-- Set achievement to completed
remote.OnServerEvent:Connect(function(client, achievementName)
    local success, errormessage = pcall(function()
        DS:SetAsync(client.UserId..achievementName, true)
    end)

    if success then
        print("Set achievement data for "..client.Name..", "..achievementName.." to true.")
        return true
    else
        warn("Unable to set achievement data")
        warn(errormessage)
        return false
    end
end)

-- Check if the achievement is completed
getCompleted.OnServerEvent:Connect(function(client, achievementName)
    local data = nil
    local success, errormessage = pcall(function()
        data = DS:GetAsync(client.UserId..achievementName)
    end)

    if success then
        print("Got data successfully.")
        return data
    else
        warn("Could not get data.")
        warn(errormessage)
        return nil
    end
end)

both events should be called by the client

1 Like

What does it mean by Event variable?

1 Like

You could use a remote event, then wherever the hook will be connected to in the client, just call

local Remote = -- define it here

local data = Remote:Invoke()

you could also put that script into a module script, ensuring that stuff is enclosed in a function

local module = {}

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("nameHere")

local remote = [the event variable here]
local getCompleted = [variable for an event to check if its completed]

-- Set achievement to completed
function module.set(client, achievementName)
    local success, errormessage = pcall(function()
        DS:SetAsync(client.UserId..achievementName, true)
    end)

    if success then
        print("Set achievement data for "..client.Name..", "..achievementName.." to true.")
        return true
    else
        warn("Unable to set achievement data")
        warn(errormessage)
        return false
    end
end

-- Check if the achievement is completed
function module.get(client, achievementName)
    local data = nil
    local success, errormessage = pcall(function()
        data = DS:GetAsync(client.UserId..achievementName)
    end)

    if success then
        print("Got data successfully.")
        return data
    else
        warn("Could not get data.")
        warn(errormessage)
        return nil
    end
end

return module

then in the client script use

local module = game:GetService("ServerScriptService"):WaitForChild(--[[define the module]])

local m = require(module)


module.get(game.Players.LocalPlayer, nameHere)

here’s my code btw its in a local script because it has a destroy local Hat =

game.Workspace.QueenoftheNight_Blue
local Active = true
Hat.ClickDetector.MouseClick:Connect(function(player)
if Active == true then
player.leaderstats.Wins.Value += 1
Hat:Destroy()
print(“You did the quest”)
Active = false
end

if Active == false then
	print("You did this quest already")
end

end)

1 Like

ok nice, in that case use the module version above

local module = game:GetService("ServerScriptService"):WaitForChild(--[[define the module]])

local m = require(module)


local active = module.get(game.Players.LocalPlayer, nameHere)

you have the active variable now!
when you want to set it to completed, do

module.set(game.Player.LocalPlayer, nameHere)