Saving bool value in a datastore

So basically what I’m going for is a datastore that saves if a bool value is true or false, the developer page only seems to cover leaderstats so instead I tried just having a leader stats datastore but with a boolvalue in replicated storage, the problem is it isn’t saving it and when you restart the game its false again, there could be several reasons why it isn’t working it could be because if I wanted it to be a bool value it’d need to be written pretty differently but I’m not too sure
code

local DataStoreService = game:GetService("DataStoreService")
local CCrackerSave = DataStoreService:GetDataStore("CCrackerSave")
local CCrackerInstallRemote = game.ReplicatedStorage.CCrackerInstall
game.Players.PlayerAdded:Connect(function(player)
	local IsCCrackerInstalled = game.ReplicatedStorage.IsCCrackerInstalled.Value
	
	local data 
	local success, errormessage = pcall(function()
		data = CCrackerSave:GetAsync(player.UserId.."-IsCCrackerInstalled")
	end)
	if success then
		IsCCrackerInstalled = data
	else
		print("Couldn't save...")
		warn(errormessage)
	end
end)

CCrackerInstallRemote.OnServerEvent:Connect(function(player)
	game.ReplicatedStorage.IsCCrackerInstalled.Value = true
	local success, errormessage = pcall(function()
		CCrackerSave:SetAsync(player.UserId.."-IsCCrackerInstalled",game.ReplicatedStorage.IsCCrackerInstalled.Value)
	end)
	if success then
		print("CCrackerInstall Saved")
	else
		print("CCrackerInstall Failed..")
		warn(errormessage)
	end
end)

Not sure how I’d start to fix it but anything helps

What you are currently doing is sharing one bool value in replicated storage for all players. You can put a bool value inside the player when they join the game if you desire, then you can save the value of that bool value to the datastore whenever needed.

Still doesn’t save but heres the new code incase I did something wrong

local DataStoreService = game:GetService("DataStoreService")
local CCrackerSave = DataStoreService:GetDataStore("CCrackerSave")
local CCrackerInstallRemote = game.ReplicatedStorage.CCrackerInstall
game.Players.PlayerAdded:Connect(function(player)
	local IsCCrackerInstalled = Instance.new("BoolValue")
	IsCCrackerInstalled.Name = "IsCCrackerInstalled"
	IsCCrackerInstalled.Parent = player
	
	local data 
	local success, errormessage = pcall(function()
		data = CCrackerSave:GetAsync(player.UserId.."-IsCCrackerInstalled")
	end)
	if success then
		IsCCrackerInstalled = data
	else
		print("Couldn't save...")
		warn(errormessage)
	end
end)

CCrackerInstallRemote.OnServerEvent:Connect(function(player)
	game.ReplicatedStorage.IsCCrackerInstalled.Value = true
	local success, errormessage = pcall(function()
		CCrackerSave:SetAsync(player.UserId.."-IsCCrackerInstalled",player.IsCCrackerInstalled.Value)
	end)
	if success then
		print("CCrackerInstall Saved")
	else
		print("CCrackerInstall Failed..")
		warn(errormessage)
	end
end)
1 Like

game.ReplicatedStorage.IsCCrackerInstalled.Value

Only returns the value overall, regardless of change.

However,

game.ReplicatedStorage.IsCCrackerInstalled

This will return the instance, and if you say [instance_name].Value, it will return the current value of that boolean instance.

1 Like

I tried this and this didn’t seem to do it either

1 Like

This is probably possible, but I normally use an int value. 0 being false, 1 being true. It’s the easiest way I know of so far.

(You may think this is un-organised, but it works!)

2 Likes

Well this script was made for an int value so I’ll try it

1 Like

okay no int value isn’t doing it either, so I think its a problem with the code itself because if I reload the game the int isn’t 1

1 Like

What’s the error? Do you have studio access to API services enabled?

I do, infact, have studio access to api services enabled, its not even giving me an error it just prints my successful message and casually doesn’t work

1 Like

You can’t save booleans, try tostring() wrapped around the argument and see if that works.

3 Likes

this gave me an error saying “attempt to call a string value” heres the code incase I put the tostring in the wrong place

local DataStoreService = game:GetService("DataStoreService")
local CCrackerSave = DataStoreService:GetDataStore("CCrackerSave")
local CCrackerInstallRemote = game.ReplicatedStorage.CCrackerInstall
game.Players.PlayerAdded:Connect(function(player)
	local CCracker = Instance.new("Folder")
	CCracker.Name = "CCracker"
	CCracker.Parent = player
	local IsCCrackerInstalled = Instance.new("BoolValue")
	IsCCrackerInstalled.Name = "IsCCrackerInstalled"
	IsCCrackerInstalled.Parent = player.CCracker
	
	local data = DataStoreService:GetDataStore(tostring(player.UserId))
	local success, errormessage = pcall(function()
		data = CCrackerSave:GetAsync(tostring(player.UserId)"-IsCCrackerInstalled")
	end)
	if success then
		IsCCrackerInstalled = data
	else
		print("Couldn't save...")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		CCrackerSave:SetAsync(tostring(player.UserId)"-IsCCrackerInstalled", player.CCracker.IsCCrackerInstalled.Value)
		wait(3)
	end)
	if success then
		print("CCrackerInstall Saved")
	else
		print("CCrackerInstall Failed..")
		warn(errormessage)
		wait(3)
	end
end)

okay I’ve added prints as well as added tostring and it worked, thank you!

5 Likes