A Variable That Is A Bool Value

Hi People,

I have been trying to save Data Using Tables And I Can`t Get Why it does not work

local GlobalStore = game:GetService("DataStoreService")
local GlobalDataSave = GlobalStore:GetDataStore("Global")
local plrsLeft = 0
local DefaultBoost = 1
local DefaultRank = "Noob"
local DefaultCap = 15

game.Players.PlayerAdded:Connect(function(player)
	
	plrsLeft = plrsLeft + 1
	
	local plrId = "User-"..player.UserId
	
	
	local plrFold = Instance.new("Folder")
	plrFold.Name = "leaderstats"
	plrFold.Parent = player
	
	local Sticks = Instance.new("NumberValue")
	Sticks.Name = "Sticks"
	Sticks.Parent = plrFold
	Sticks.Value = 0
	
	local Coins = Instance.new("NumberValue")
	Coins.Name = "Coins"
	Coins.Parent = plrFold
	Coins.Value = 0
	
	local Class = Instance.new("StringValue")
	Class.Name = "Class"
	Class.Value = DefaultRank
	Class.Parent = plrFold
	
	local Caps = Instance.new("NumberValue")
	Caps.Parent = Sticks
	Caps.Name = "MaxCapacity"
	Caps.Value = DefaultCap
	
	local BoostRank = Instance.new("NumberValue")
	BoostRank.Name = "ClassBoost"
	BoostRank.Parent = Class
	BoostRank.Value = DefaultBoost
	
	local X2Sticks = Instance.new("NumberValue")
	X2Sticks.Name = "X2Sticks"
	X2Sticks.Value = 1
	X2Sticks.Parent = Sticks
	
	local X2Coins = Instance.new("NumberValue")
	X2Coins.Name = "X2Coins"
	X2Coins.Parent = Coins
	X2Coins.Value = 1
	
	local CachedData = {
		[123456789] = {
			Coins = Coins.Value;
			Sticks = Sticks.Value;
			Caps = Sticks.MaxCapacity.Value;
			Classw = Class.Value;
			ClassBoost = BoostRank.Value;
			X2Stick = X2Sticks.Value;
			X2Coin = X2Coins.Value
			
			}
		
		
	}
	local Data = pcall(function()
		GlobalDataSave:GetAsync(plrId, function(GotData)
			return CachedData[plrId]
		end)
	end)
	
	if Data then
		
		warn("Data Was Found!")
		
		player.leaderstats.Sticks.Value = Data[2]
		player.leaderstats.Coins.Value = Data[1]
		player.leaderstats.Class.Value = Data[4]
		player.leaderstats.Sticks.MaxCapacity.Value = Data[3]
		player.leaderstats.Sticks.X2Sticks.Value = Data[7]
		player.leaderstats.Coins.X2Coins.Value = Data[6]
		player.leaderstats.Class.ClassBoost.Value = Data[5]
	end
end)

local BindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	
	local playerLb = player:WaitForChild("leaderstats") -- so no errors so we just wait for the child instead finding it as the first child
	
	local plrId = "User-"..player.UserId
	
	local CachedData = {
		[123456789] = {
			playerLb.Sticks.Value;
			playerLb.Coins.Value;
			playerLb.Class.Value;
			playerLb.Sticks.MaxCapacity.Value;
			playerLb.Class.ClassBoost.Value;
			playerLb.Sticks.X2Sticks.Value;
			playerLb.Coins.X2Coins.Value
			}
		
		}
	local Succes, ErrorMessage = pcall(function()
		GlobalDataSave:OnUpdate(plrId, function(OldData)
			return CachedData[plrId]
		end)
	end)
	
	pcall(function()
		plrsLeft = plrsLeft - 1
		BindableEvent:Fire()
	end)
end)

game:BindToClose(function()
	while plrsLeft > 0 do
		BindableEvent.Event:Wait()
	end
end)

if u say BindToClose is the issue it is not

the issue is on line 78 with variable Data and it says a boolean value

Data is currently being set to if the pcall ran successfully, I believe you meant to do:

local Success, Data = pcall(function()

GetAsync only takes a Key argument, plus this should return the GetAsync (otherwise the value GetAsync returns will not go anywhere).


Instead of returning the GetAsync though, you could just pass over the GetAsync function into the pcall, but remember : counts as if you’re calling the function so you must pass it with . and provide self.

For example:

local Success, Data = pcall(GlobalDataSave.GetAsync, GlobalDataSave, plrId)

Do note that Data could be the error message if the pcall fails!

1 Like

u know that success can only be implied with a variable name called ErrorMessage
Also .getAsync wont work since its a considered DataStore function

The first argument is if the pcall successfully ran, and it’s always that.
Variable names never make a difference to functionality, only readability.

.GetAsync does work, I use it myself.

I agree, I always use GetAsync

Save the bool value as a string of “true” or “false”

its a variable not a specific boolean value

but im trying to save it on players user id with a written function called GotData

Im just gonna switch to DataStore2 instead of this

*ugh why are datastores are so complicated