Saving a boolvalue in a player

Hey there,
I had a script that was meant to save a boolvalue to a player but it made my leaderstats stop saving for some reason so I deleted it but I was just wondering if anybody could help me create a script to save a boolvalue that wont wreck my leaderstats. Thanks

What are the scripts that broke?

1 Like

Make sure you are saving the actual value, and not the instance itself. It should work like any other number or string.

1 Like

Ill go try it again, ill get back to you

It was a leaderstat script that had nothing to do with the other script

Well it isnt breaking the leaderstats script now but it still wont save

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("CashData")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "OwnedSkins"
	local Skull= Instance.new("BoolValue", Leaderstats)
	Skull.Name = "Skull"
	Skull.Value = false
	local GalaxyWonderer= Instance.new("BoolValue", Leaderstats)
	GalaxyWonderer.Name = "GalaxyWonderer"
	GalaxyWonderer.Value = false

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Skull.Value = Data.Skull 
		GalaxyWonderer.Value = Data.GalaxyWonderer 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Skull"] = Player.OwnedSkins.Skull.Value; 
		["GalaxyWonderer"] = Player.OwnedSkins.GalaxyWonderer.Value; 
	})
end)

First of All, You should
Make Sure it has Enough Time to actually Save the Data.

Add this at the end of the Script

game:BindToClose(function()
    if game:GetService("RunService"):IsStudio() then
       wait(1)
   else
       for _, localplayer in pairs(game:GetService("Players"):GetPlayers()) do
          local Success, Error = pcall(function()
              DataStore:SetAsync(localplayer .UserId, 
                ["Skull"] = localplayer.OwnedSkins.Skull.Value; 
	        	["GalaxyWonderer"] = localplayer.OwnedSkins.GalaxyWonderer.Value;
              }) 
          end)
          if not Success then warn(Error) end
       end
    end
end)

Should i replace that with the bottom bit??

No, Just add it below the bottom.

1 Like

okay thank you :slight_smile: ill go try that

1 Like

I got this error

  18:14:03.377  ServerScriptService.SaveSkins:33: Expected identifier when parsing expression, got '['  -
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("CashData")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "OwnedSkins"
	local Skull= Instance.new("BoolValue", Leaderstats)
	Skull.Name = "Skull"
	Skull.Value = false
	local GalaxyWonderer= Instance.new("BoolValue", Leaderstats)
	GalaxyWonderer.Name = "GalaxyWonderer"
	GalaxyWonderer.Value = false

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Skull.Value = Data.Skull 
		GalaxyWonderer.Value = Data.GalaxyWonderer 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Skull"] = Player.OwnedSkins.Skull.Value; 
		["GalaxyWonderer"] = Player.OwnedSkins.GalaxyWonderer.Value; 
	})
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		wait(1)
	else
		for _, localplayer in pairs(game:GetService("Players"):GetPlayers()) do
			local Success, Error = pcall(function()
				DataStore:SetAsync(localplayer.UserId, {
					["Skull"] = localplayer.OwnedSkins.Skull.Value; 
					["GalaxyWonderer"] = localplayer.OwnedSkins.GalaxyWonderer.Value; 
				})
			end)
		end
	end
end)

It didn’t work unfortunately but I did get this

18:17:47.527  DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key =

this means the game is saving data too frequently.
Is this your only data storing script or?

1 Like

No, I’m saving leaderstats too

So Are you using two different scripts for them?

1 Like

just 1 script for my leaderstats and 1 for this

You dont need to use seperate scripts.
Just use 1 Single script to do both of them.

Can you show your leaderstats Script?

1 Like

actually i already fix on this topic!
How can I stop getting Data Store warnings on my Data store script? - Help and Feedback / Scripting Support - DevForum | Roblox

And i think, why not saving a numbervalue as a boolvalue instead? like you setting value 1 to true and value 0 to false

That’s not the Issue.
BindToClose Just Is Used for when the Server is Closing.

I Already sent A Script with that.
The Issue Here is data is getting saved Multiple times (In Different Scripts).

Simple way to fix is to Use a table containing all the Instances to Save
in a single Script.