Saving a boolvalue in a player

Sure just wait one moment ill send it

here you are

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

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Cash= Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash"
	Cash.Value = 0
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0

	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Cash.Value = Data.Cash 
		Kills.Value = Data.Kills 
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {
		["Cash"] = Player.leaderstats.Cash.Value; 
		["Kills"] = Player.leaderstats.Kills.Value; 
	})
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("playerInstances")

game.Players.PlayerAdded:Connect(function(Player)
	local OwnedSkinsFolder = Instance.new("Folder", Player)
	OwnedSkinsFolder.Name = "OwnedSkins"
	
	local Skull= Instance.new("BoolValue", OwnedSkinsFolder)
	Skull.Name = "Skull"
	Skull.Value = false
	
	local GalaxyWonderer= Instance.new("BoolValue", OwnedSkinsFolder)
	GalaxyWonderer.Name = "GalaxyWonderer"
	GalaxyWonderer.Value = false
	
	-- // Leaderstats 
	
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	
	local Cash= Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash"
	Cash.Value = 0
	
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	
	local gotData
	local Success,Errormsg = pcall(function()
		gotData = DataStore:GetAsync(Player.UserId)
	end)
	if gotData then
		Kills.Value = gotData.Kills
		Cash.Value = gotData.Cash
		Skull.Value = gotData.Skull
		GalaxyWonderer.Value = gotData.GalaxyWonderer
	else
		print("New Player Joined / No Data")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success,Errormsg = pcall(function()
    DataStore:SetAsync(Player.UserId, {
		["Skull"] = Player.OwnedSkins.Skull.Value; 
		["GalaxyWonderer"] = Player.OwnedSkins.GalaxyWonderer.Value; 
		["Cash"] = Player.leaderstats.Cash.Value;
		["Kills"] = Player.leaderstats.Kills.Value;
	})
     if not Success then warn(Errormsg) end
    end)
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		wait(1.25)
	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; 
					["Cash"] = localplayer.leaderstats.Cash.Value;
					["Kills"] = localplayer.leaderstats.Kills.Value;
				})
			end)
		end
	end
end)
3 Likes

if gotData and Success then isnā€™t a good if statement, use if Success then instead since itā€™s a pcall functionā€¦

It worked thank you so much!!!

Hey are the datastores experiencing issues or something because sometimes my stats will save and others it wont.

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

game.Players.PlayerAdded:Connect(function(Player)
	local OwnedSkinsFolder = Instance.new("Folder", Player)
	OwnedSkinsFolder.Name = "OwnedSkins"
	
	local Skull= Instance.new("BoolValue", OwnedSkinsFolder)
	Skull.Name = "Skull"
	Skull.Value = false
	
	local GalaxyWonderer= Instance.new("BoolValue", OwnedSkinsFolder)
	GalaxyWonderer.Name = "GalaxyWonderer"
	GalaxyWonderer.Value = false
	
	-- // Leaderstats 
	
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	
	local Cash= Instance.new("IntValue", Leaderstats)
	Cash.Name = "Cash"
	Cash.Value = 0
	
	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	
	local gotData
	local Success,Errormsg = pcall(function()
		gotData = DataStore:GetAsync(Player.UserId)
	end)
	if gotData then
		Kills.Value = gotData.Kills
		Cash.Value = gotData.Cash
		Skull.Value = gotData.Skull
		GalaxyWonderer.Value = gotData.GalaxyWonderer
	else
		print("New Player Joined / No Data")
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Success,Errormsg = pcall(function()
    DataStore:SetAsync(Player.UserId, {
		["Skull"] = Player.OwnedSkins.Skull.Value; 
		["GalaxyWonderer"] = Player.OwnedSkins.GalaxyWonderer.Value; 
		["Cash"] = Player.leaderstats.Cash.Value;
		["Kills"] = Player.leaderstats.Kills.Value;
	})
     if not Success then warn(Errormsg) end
    end)
end)

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then
		wait(1.25)
	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; 
					["Cash"] = localplayer.leaderstats.Cash.Value;
					["Kills"] = localplayer.leaderstats.Kills.Value;
				})
			end)
		end
	end
end)
1 Like

It still wont save sometimes, well most times

well does it print anything in output when it does not save.
It should if it cant save.

1 Like

nope sorry it only prints ā€œnilā€

Everything wrapped in a pcall? If not take a look at pcalls and wrap the save data and get data things in one. If data store services ever go down then your gameā€™s data will still save and your game will be fine.

1 Like

I think they were, Iā€™m not too sure tho

1 Like

Well Yea, Everything is In A Pcall.

The Issue Comes in Loading Apparently.
If There is an error Saving, It would print it out but it does not.

That means its erroring while loading or somethin?

2 Likes

Must be, I dont see anything wrong with it tho

1 Like