Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Datastore boolvalue not working?

Hi guys! For some reason, this script is not saving (it creates the boolvalue, though.) I am trying to get it to constantly save the value of the boolvalue to the datastore. Any ideas? Thanks

one = true two =false
game.Players.PlayerAdded:connect(function(player)
local store = game:GetService("DataStoreService"):GetDataStore("awesomedata")
local BV = Instance.new("BoolValue",player)
BV.Value = false
BV.Name = "lol"
local key="user_"..player.userId
store:SetAsync(key, {one, two})
function unscramble(input)
if input==1 then return true
else return false
end
end

local content1=store:GetAsync(key,{one})
local unscrambled=unscramble(content1)
end)

3 answers

✓ Accepted answer

I'm not exactly sure what your intentions were with this script but:

First of all you cannot save a table. You will have to use the JSON format. Also you try to compare a boot value to 1. Third you never actually load any value.

Your script:

game.Players.PlayerAdded:connect(function(player)
	local store = game:GetService("DataStoreService"):GetDataStore("awesomedata")
	local key="user_"..player.userId
	local BV = Instance.new("BoolValue",player)
	BV.Name = "lol"
	BV.Value = store:GetAsync(key) or false
	
	BV.Changed:connect(function()
		store:SetAsync(key,BV.Value)
	end)
end)

Your script saving multiple values using JSON.

local Utils = LoadLibrary("RbxUtility")
game.Players.PlayerAdded:connect(function(player)

	local store = game:GetService("DataStoreService"):GetDataStore("awesomedata")
	local key="user_"..player.userId

	local BV = Instance.new("BoolValue",player)
	BV.Name = "lol"
	local BV2= Instance.new("BoolValue",player)
	BV2.Name = "lol2"

	Save = store:GetAsync(key) and Utils.DecodeJSON(GetAsync(Key)) 
	
	if Save then
		BV.Value = Save[1]
		BV2.Value = Save[2]
	else
		BV.Value = true
		BV2.Value = false
	end 

	function SaveValue()
		store:SetAsync(key,EncodeJSON({BV.Value;BV2.Value}))
	end
	BV.Changed:connect(SaveValue)
	BV2.Changed:connect(SaveValue)
end)

An automatic DataStore script.

local Data = {
	["Bool Value 1"] =  {"BoolValue"; true};
	["Points"] = {"IntValue"; 100};
}

local DataStoreService = Game:GetService("DataStoreService")

Game.Players.PlayerAdded:connect(function(Player)
	local UserId = Player.userId
	local Folder = Instance.new("Folder",player)
	Folder.Name = "Stats"
	for Name,Info in pairs(Data) do
		local Type, Default = unpack(Info)
		local Store = DataStoreService:GetDataStore(Name)
		local Stat = instance.new(Type,Folder)
		Stat.Name = Name
		local Save = Store:GetAsync(UserId)
		Stat.Value = Save or Default
		Stat.Changed:connect(function()
			Store:SetAsync(UserId,Stat.Value)
		end)
	end
end)
local one = true --consider using local variables
local two =false

game.Players.PlayerAdded:connect(function(player)
local store = game:GetService("DataStoreService"):GetDataStore("awesomedata")
local BV = Instance.new("BoolValue",player)
BV.Value = false
BV.Name = "lol"
local key="user_"..player.userId
store:SetAsync(key, {one, two}) --ok so we set {true,false} to the datastore with key
local function unscramble(input) --consider local function
if input==1 then --input in this case is a table, not an int.
return true
else
return false
end
end

local content1=store:GetAsync(key) --returns the value of key in the datastore 'store'
local unscrambled=unscramble(content1) --here's your problem, content1 is a table, not an int.
end)

content1 would equal {true,false} so i don't know what you want to do with these bool values, but implement it into your code.

Some code I used to save a BoolValue.

Note that it isn't under typical "leaderstats" value. Simple edit though.

function onPlayerEntered(player)
wait()
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("Player_Preferences") --Change to where you BoolValue is parented under
if player.DataReady then
if player:findFirstChild("Player_Preferences") then
local score = player.Player_Preferences.Reset --BoolValue.Name here
local ScoreLoaded = player:LoadBoolean(score.Name)
wait()
if ScoreLoaded ~= false then
score.Value = ScoreLoaded
end
end
end
end


function onPlayerLeaving(player)
if player:findFirstChild("Player_Preferences") then
local score = player.Player_Preferences.Reset
player:SaveBoolean(score.Name,score.Value)
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeaving)

This discussion is preserved as it was. New answers, comments, and votes are closed.