You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Store Boolean Values in a datastore
What is the issue? Include screenshots / videos if possible!
When I attempt to store a boolean in a datastore using GlobalDataStore, it never works.
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)
if Saved then
save1.Value = Saved[1]
save2.Value = Saved[2]
else
local NumberForSaving = {save1.Value, save2.Value}
ds:SetAsync(plrkey, NumberForSaving)
end
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
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)
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)
@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