DataStore help needed

Hello fellow developers.
My Data Store doesn’t work.
All help appreciated :slight_smile:

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-coins","-gems")
	end)
	
	if success then
		coins.Value = data
		gems.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(player.UserId.."-coins",player.leaderstats.coins.Value,"-gems",player.leaderstats.gems.Value)
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving the data")
		warn(errormessage)
	end
	
end)

Error:

coins is not a valid member of Folder “Players.Luxurious_Scripter.leaderstats”

1 Like

Where is the line where the script breaks?

1 Like

It says line 42 with a warn message

1 Like

Your GetAsync() call seems out of syntax. You’re trying to get the value “-gems” from the key player.UserId…"-coins", is this correct?
Would you need to do two calls for GetAsync(player.UserId, "-coins") and GetAsync(player.UserId, "-gems") separately? You can’t fetch/set two values at once.

Part where error occurred (warn message)

1 Like

@Luxurious_Scripter

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-coins","-gems")
	end)
	
	if success then
		coins.Value = data
		gems.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value,"-gems",player.leaderstats.gems.Value) --- did lowercase C here
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving the data")
		warn(errormessage)
	end
	
end)

you did coins with a lowercase c instead of a capital C lol. copy paste the code and you’ll be good

The error happens because you created it with a capital C and called with a lowercase C

Try doing palyer.leaderstats:WaitForChild(“coins”).Value instead

it’s because he named coins with a capital C here

but called it with a lowercase one

1 Like

What about the pcall for coins and gems?

Not sure what you’re asking but if you’re asking about the gems it has a lowercase G too while its meant to be uppercase.

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		data = dataStore:GetAsync(player.UserId.."-coins","-gems")
	end)
	
	if success then
		coins.Value = data
		gems.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(player.UserId.."-coins",player.leaderstats.Coins.Value,"-gems",player.leaderstats.Gems.Value) --- did lowercase C here and lowercase G
	end)
	
	if success then
		print("Player Data successfully saved!")
	else
		print("There was an error when saving the data")
		warn(errormessage)
	end
	
end)
1 Like