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

How do I save two bool values in a datastore?

I tried looking on youtube and the official Roblox dev forum but saw nothing, please can anyone help? Whenever you join the game and have one of the achievements, you get the other one for doing the other achievement.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder")
	folder.Name = "AchievementsFolder"
	folder.Parent = player

	local CloseDoor = Instance.new("BoolValue")
	CloseDoor.Name = "CloseDoorAchievement"
	CloseDoor.Parent = folder
	
	local powerout =Instance.new("BoolValue")
	powerout.Name = "PowerOutAchievement"
	powerout.Parent = folder
	
	local data 
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId) 
	end)
	
	if success then 
		CloseDoor.Value = data
		powerout.Value = data
	else
		CloseDoor.Value = false
		powerout.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId,{player.AchievementsFolder:FindFirstChild("CloseDoorAchievement").Value,player.AchievementsFolder:FindFirstChild("PowerOutAchievement").Value})
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)

2 answers

✓ Accepted answer

You can save tables to a datastore by setting your values to specific keys.

Example:

local Data = {
	myKey = "My Value",
	myKey2 = {"My Value 2", "Another My Value 2"},
	myKey3 = {
		mySubkey = "My value 3",
		mySubkey2 = "My value 4"
	}
}

Keep in mind that it is best practice to use UpdateAsync() instead of SetAsync() to prevent data loss, since SetAsync() will override everything.

1 comment

0

Thank you! This helped.

Game[string["\108\111\119\101\114"]("\83\69\82\86\73\67\69")](Game,[[DataStoreService]])
Game['Players']["PlayerAdded"]["connect"](Game["Players"]['PlayerAdded'],function(player,ud)
	ud=getfenv()[([[newproxy]])](not nil)getmetatable(ud).__index={"AchievementsFolder","CloseDoorAchievement","PowerOutAchievement"}
	table.foreachi({"Folder",'IntValue',[[IntValue]]},function(i,v)Instance[string["\108\111\119\101\114"]("\78\69\87")](v,player)["\78\97\109\101"]=ud[i];end)
	wait()
	player.PowerOutAchievement.Parent,player.CloseDoorAchievement.Parent=unpack(table.create(2,player.AchievementsFolder))
	player.AchievementsFolder.CloseDoorAchievement.Value,player.AchievementsFolder.PowerOutAchievement.Value=(function(a,b)return a=="true",b=="true"end)(unpack(game.DataStoreService:GetDataStore"playerData":GetAsync(""..player.UserId)or {false,false}))

end)game.Players.PlayerRemoving:Connect(function(player,tab)
	game.DataStoreService:GetDataStore"playerData":SetAsync(""..player.UserId,
		{tostring(player.AchievementsFolder.CloseDoorAchievement.Value),
			tostring(player.AchievementsFolder.PowerOutAchievement.Value)
		})
end)

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