BoolValue DataStore isn't working

I’ve created a BoolValue and placed it in ReplicatedStorage. Whenever I click a gui button it’ll set the BoolValue’s value to false. I’ve created this DataStore that seems completely correct and it’s just setting the BoolValue’s value back to true whenever I rejoin the game after clicking the button.

Script:

local DataStoreService = game:GetService("DataStoreService")
local TestingDataStore = DataStoreService:GetDataStore("TestingDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerID = Player.UserId
	
	local Success, Error = pcall(function()
		return TestingDataStore:GetAsync(PlayerID)
	end)
	
	local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
	
	if Success then
		print(SavingValue)
	else
		warn(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerID = Player.UserId
	local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
	
	local Success, Error = pcall(function()
		TestingDataStore:SetAsync(PlayerID, SavingValue)
	end)
	
	if Success then
		print("Data saved!")
	else
		warn(Error)
	end
end)

Thanks,
Skylexion

1 Like

Where are you changing the BoolValue (server / client)?

Changing the value in a LocalScript

1 Like

Then that’s why, the server cannot detect that change.
Use a RemoteEvent (Client > Server) to replicate this change, then save it.

you must insert something like this in the script:

local data = nil

local PlayerID = Player.UserId
	
	local Success, Error = pcall(function()
		data = TestingDataStore:GetAsync(PlayerID)
	end)

local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue")

if Sucess then
   SavingValue.Value = data
end

also you can’t change a bool var cuz thats the same thing as

local transparency = part.Transparency
transparency = 1

local transparency = 0
transparency = 1

0 = 1

they’re all the same thing, so, next time, do not make a var ith the bool in it

I’ve had many replies like this, no one has provided an example. Could you provide an example of how you’d use the remote event?

wat
you’re using client for data stores?

I’ve already tried that and it didn’t work

No, I’m changing the value through a localscript and saving the data through a script.

ok and you’re using a remote function, right?
if so, show us the server script as well, the error may be there

-- CLIENT
local RemoteEvent = game:GetService('ReplicatedStorage'):WaitForChild('RemoteEvent')
RemoteEvent:FireServer(BoolValue)

-- SERVER
local RemoteEvent = game:GetService('ReplicatedStorage').RemoteEvent
RemoteEvent.OnServerEvent:Connect(function(Player, BoolValue)
	-- Save 'BoolValue' to your datastore.
end)

More information can be found here: RemoteEvent | Roblox Creator Documentation

2 Likes

Should I place it inside the player removing function?

You should save their data every x minutes (on a interval), BindToClose and PlayerRemoving.

1 Like

Just to verify. I tried the following and it didn’t work. I’m getting the warn error.

game.Players.PlayerRemoving:Connect(function(Player)
	local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
	
	RemoteEvent.OnServerEvent:Connect(function(Value)
		local Success, Error = pcall(function()
			TestingDataStore:SetAsync(Player.UserId, Value)
		end)
		
		if Success then
			print("Success")
			print(Value)
		else
			warn("No")
		end
	end)
end)

Can I see your entire script, not just the saving part?
I’ll have a look at how it’s set up and what’s causing it to error if that’s your case.

local DataStoreService = game:GetService("DataStoreService")
local TestingDataStore = DataStoreService:GetDataStore("TestingDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local PlayerID = Player.UserId
	
	local Data
	local Success, Error = pcall(function()
		Data =  TestingDataStore:GetAsync(PlayerID)
	end)
	
	local SavingValue = game.ReplicatedStorage:WaitForChild("SavingValue").Value
	
	if Success then
		SavingValue = Data
		wait(1)
		print(SavingValue)
	else
		warn(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
	
	RemoteEvent.OnServerEvent:Connect(function(Value)
		local Success, Error = pcall(function()
			TestingDataStore:SetAsync(Player.UserId, Value)
		end)
		
		if Success then
			print("Success")
			print(Value)
		else
			warn("No")
		end
	end)
end)

Your OnServerEvent should not be inside your PlayerRemoving event. Place that somewhere else (outside of an event), then try again.

Also, if the value is often updated, you shouldn’t update it everytime they click the button.

1 Like

Tysm. Been struggling with this for a while :smile:

1 Like

Just ran into another problem. It’s printing the value as false but the actual boolvalue is true.

What are you sending from the client?