How to save BoolValue in DataStore?

I’m using script. Part → ProximityPrompt → Script.

try this

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	local gate1 = Instance.new("BoolValue")
	gate1.Name = "Gate1"
	gate1.Parent = plr
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
		myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value) -- BOOLVALUE
	end)
	if err then
		warn(err)
	end
end)

Change whatever you need, just make sure the keys for getting AND setting the coins and bool are different.

If this is correct mark @Lukewave7552’s first response as the answer, since he’s the person that originally said this.

Now there is no error, this is good, but the data of boolean is not saved.

Oh, sorry, I was wrong. The error still didn’t go away.

Sorry, I didn’t take the date of the store correctly. I fixed the bug, but the boolean does not remain the same.

1 Like

I am not getting any errors like that testing the same way you are.

Here is the new script I just wrote that is fully working when I test it.

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	local success,val = pcall(function()
		return myData:GetAsync(tostring(plr.UserId).."Coins")
	end)
	if success then
		coins.Value = val
	end
	
	local gate1 = Instance.new("BoolValue")
	gate1.Name = "Gate1"
	gate1.Parent = plr
	local success,val = pcall(function()
		return myData:GetAsync(tostring(plr.UserId).."Gate1")
	end)
	if success then
		gate1.Value = val
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Coins",plr.leaderstats.Coins.Value)
	end)
	if not success then
		warn(err)
	end
	local success,err = pcall(function()
		myData:SetAsync(plr.UserId.."Gate1",plr.Gate1.Value)
	end)
	if not success then
		warn(err)
	end
end)

Notes:
I’m not certain of this but I think you’re supposed to separate the SetAsync pcalls.
You HAVE to have “return” whenever you pcall GetAsync, which was why this response took so long, I just couldn’t figure that one part out.

Oops didn’t see this, but I believe my script still applies to this since mines working fully.
Also make sure you’re changing the values of what you save on the server side or it won’t save.

1 Like

Yeah, I think too!. But Boolean as before not saving.

I don’t know what can be wrong…

Could you send the SetAsync and GetAsync lines for the bool in your script?

Sorry again! The data seems to be saved, I’m not sure yet, I just checked the wrong thing. I just need to fix something. If the data is not saved, I will definitely inform you about it!

1 Like

Wait so what was the problem? I’m intrigued.

2 Likes

Just not correctly checking value of Boolean.

1 Like

I told you! Anyways good job. :slight_smile:

2 Likes

@Lukewave7552 @Hqsuperlabgames

Phew, everything seems to have worked out, and now I’m sure the data is saved! Thank you to everyone who helped me! Consider that you are the solutions, but it is a pity that only one thing can be noted, but I will note the main thing!

3 Likes