How to save bool value after player lefted server?

Hello developers!

As you read the title, you should know my question. Let me explain more.

Imagine every player in-game has bool value in their local player.

Bool value named: “PlayedGame”.

I already made a script when a player joins, the bool value set true.

But it won’t save value forever. Like when a player leaves the game, the bool value returns to false. So how can I save bool value?

Thank you for reading! :star:

1 Like

Use data store service.
DataStoreService (roblox.com)

1 Like

I already know about datastore, but I don’t know how to use it for bool values.

Use Data:SetAsync({true}), most not supported values for SetAsync can be saved with wrapping them inside a table but I do not recommend making a lot of storages for different values, just wrap them all inside a one table and save that table in only one storage.

Create an instance.new Boolean value and add that to your datastore
–By the way, this is my first time to reply in dev forum

That wouldn’t work, DataStores cannot save booleans, you need to wrap them in a table.

Oh yes, I was about to make an example

DataStores can save booleans because they can be serialised into a string format. Anything that’s serialisable into a string can be saved as a raw value to a DataStore or you can pass a table if you’re looking to store more than one value. Table wrapping is unnecessary for single-value DataStores.

1 Like

local function createStatsTable(player)
local playerStatsTable = {}
if player.leaderstats:GetChildren() then
for i, stat in pairs(player.leaderstats:GetChildren()) do
playerStatsTable[stat.Name] = stat.Value
end
end

return playerStatsTable

end

Weird, I tried saving booleans or strings in DataStore but did not work, but I already edited my post that provides the reasons why you should wrap every value that could be saved inside a table in only one DataStorage

Here a simple code i have made,

game.Players.PlayerAdded:connect(function(Ply)
	local DS = game:GetService("DataStoreService") -- get datastoreservice
	local plyhasjoined = DS:GetDataStore("PlyHasJoined" .. Ply.UserId) --get the datastore
	if plyhasjoined:GetAsync("Value") == nil then --verify if the async VALUE is nil (if player never joined)
		print("player is new !")
		plyhasjoined:SetAsync("Value", true)
	else -- else if player has arledy joined
		print("player arledy joined !")
	end
end)

(sorry for my terible English)

2 Likes

this is what I wrote. why do you copying it ?

Ok, I try every code, which worked I mark as a solution.