Hello fellow developers.
My Data Store doesn’t work.
All help appreciated
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”
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.
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
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)