How can I store a BooleanValue in a DataStore?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Store Boolean Values in a datastore

  2. What is the issue? Include screenshots / videos if possible!
    When I attempt to store a boolean in a datastore using GlobalDataStore, it never works.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked at other devforum posts but none of them fixed my problem and most of them didn’t target my exact problem/question

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()
game.Players.PlayerAdded:Connect(function(plr)
	--create a leaderstats folder for the player
	local userMapInfo = Instance.new("Folder")
	userMapInfo.Name = "userMapInfo"
	userMapInfo.Parent = plr

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	--add a new leaderstat named wins
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local map1Comp = Instance.new("BoolValue")
	map1Comp.Name = "compMap1"
	map1Comp.Value = 0
	map1Comp.Parent = userMapInfo

	wait()
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Wins
	local save2 = plr.userMapInfo.compMap1

	local Saved = ds:GetAsync(plrkey)
	if Saved then
		save1.Value = Saved[1]
		save2.Value = Saved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end

	while task.wait(1) do
		plr.leaderstats.Wins.Value += 1
	end
	
	if plr.userMapInfo.compMap1 then
		print("found")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.userMapInfo.compMap1.Value = not plr.userMapInfo.compMap1.Value
	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Wins.Value,  plr.userMapInfo.compMap1.Value})
end)
4 Likes
if Saved then
	save1.Value = Saved[1]
	save2.Value = Saved[2]
else
	local NumberForSaving = {save1.Value, save2.Value}
	ds:SetAsync(plrkey, NumberForSaving)
end

You forgot to do :SetAsync.

Before I see if this is the fix, the other value saves (save1), if this was the problem then both values wouldn’t save right?

1 Like

Process works the exactly like any other values, its just the value that changes, you’ll have to set it as false or true

Here :

local ds = game:GetService("DataStoreService"):GetGlobalDataStore()
game.Players.PlayerAdded:Connect(function(plr)

	local userMapInfo = Instance.new("Folder")
	userMapInfo.Name = "userMapInfo"
	userMapInfo.Parent = plr

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local map1Comp = Instance.new("BoolValue") -- im assuming this is what you want
	map1Comp.Name = "compMap1"
	map1Comp.Value = false -- true/false
	map1Comp.Parent = userMapInfo
	wait()
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Wins
	local save2 = plr.userMapInfo.compMap1

	local Saved = ds:GetAsync(plrkey)
	if Saved then
		save1.Value = Saved[1]
		save2.Value = Saved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end

	while task.wait(1) do
		plr.leaderstats.Wins.Value += 1
	end
	
	if plr.userMapInfo.compMap1.Value then
		print("found")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	plr.userMapInfo.compMap1.Value = not plr.userMapInfo.compMap1.Value
	ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Wins.Value, plr.userMapInfo.compMap1.Value})
end)

little tip : Make consistent variable names & optimize this script

Yes, in your current code, you didn’t save the values.

1 Like

The int value saves, the bolean value does not save tho.

1 Like

I don’t c any BoolValue in your code.

1 Like

That was my bad, the code I provided was from trying to see if I could just use a intvalue instead of a boolvalue. It didn’t work either

Which value should be boolvalue?

map1Comp should have been the boolean Value

Using :SetAsync when the player joins will ensure that if the data is not found for the player, it will be saved to the datastore, Also you do not store any BoolValue in your code, so the title does not make any sense, as both of your settings are IntValue Like Jqck said, I’ve rewrote your script.

local DataStoreService = game:GetService("DataStoreService")
local GlobalDataStore = DataStoreService:GetGlobalDataStore()
local KEY_ID = "id_"

game.Players.PlayerAdded:Connect(function(Player)
	local userMapInfo = Instance.new("Folder")
	userMapInfo.Name = "userMapInfo"
	userMapInfo.Parent = Player

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player

	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats

	local compMap1 = Instance.new("BoolValue")
	compMap1.Name = "compMap1"
	compMap1.Value = false
	compMap1.Parent = userMapInfo

	wait()
	
	local PlayerKey = KEY_ID .. Player.UserId
	local save1 = Player.leaderstats.Wins
	local save2 = Player.userMapInfo.compMap1

	local Saved = GlobalDataStore:GetAsync(PlayerKey)
	
	if Saved then
		save1.Value = Saved[1]
		save2.Value = Saved[2]
	else
		local dataForSaving = {save1.Value, save2.Value}
		GlobalDataStore:SetAsync(PlayerKey, dataForSaving)
	end

	while true do
		wait(1)
		Player.leaderstats.Wins.Value += 1
	end

	if Player.userMapInfo.compMap1.Value then
		print("found")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	Player.userMapInfo.compMap1.Value = not Player.userMapInfo.compMap1.Value
	local PlayerKey = KEY_ID .. Player.UserId
	local dataForSaving = {Player.leaderstats.Wins.Value, Player.userMapInfo.compMap1.Value}
	GlobalDataStore:SetAsync(PlayerKey, dataForSaving)
end)
1 Like

I just edited the change to make it a BoolValue. Does the datastore only work with leaderstats? That is the only thing I can assume that would be the solution here. I’ve added the changes you and Jqck have suggested and I am having the same problem. (thank you Meta and Jqck)

1 Like

Seems like @xvywop cracked the code before I did. His code should work.

Use the code i provided, it should work as intended if not, please let me know.

Remember to change the value of compMap1 to false.

@xvywop & @Mystxry12
If I set the value to true in place #2, and rejoin place #1 the value is infact stored. The user cannot go DIRECTLY back to place #1 via a teleport from place #2 if that makes sense. Thank you both for the help you’ve provided, it really means a lot to me :slight_smile:

Consider marking one of our comments as a

solution

so that people don’t come in here and reply as the issue is solved.

Haha that is a good idea lol, will do.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.