Hello. I'm trying to use Datastores to save the data of the player but I'm getting this error: 13:23:18.407 - leaderstats is not a valid member of Player
My code:~~~~~~~~~~~~~~~~~
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "Leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = MyDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("There was an error while getting your data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() MyDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value) end) if success then print("Player data succesfully saved") else print("There was an error when saving your data") warn(errormessage) end
end)

3 comments
okay. thank you
can you accept my answer? thank you :)
Done