Hey! I am making a new project (probs would never be complete like most of em), and this is my first time needing and making a datastore. Like an average person would do that doesn’t know something, go on youtube and watch a tutorial, change some variables, modify code to their need, and rewrite the code a tiny bit. Now for some reason, the datastore isn’t work and I am getting an error from local LdrStats = Player:FindFirstChild("leaderstats")
which is weird because I defined player 2 times. Thanks!
Also, the tutorial is not outdated. Trust me, I checked.
-- Leaderstats which saves the rank & cash.
-- Service Calls
ds = game:GetService("DataStoreService"):GetDataStore("PlayerSave")
-- Functions
game.Players.PlayerAdded:Connect(function(Player)
----------- LEADERSTATS -----------
local leaderstats = Instance.new("Folder", Player)
leaderstats.Name = "leaderstats"
local Wallet = Instance.new("IntValue", leaderstats)
Wallet.Name = "Wallet"
Wallet.Value = 500
local Bank = Instance.new("IntValue", leaderstats)
Bank.Name = "Bank"
Bank.Value = 1000
local Rank = Instance.new("IntValue", leaderstats)
Rank.Name = "Rank"
Rank.Value = 0
----------- DATASTORE -----------
local Playerkey = Player.UserId
local PlayerData
local Success, ErrorMsg = pcall(function()
PlayerData = ds:GetAsync(Playerkey)
end)
if Success then
if PlayerData then
Bank.Value = PlayerData[1]
Wallet.Value = PlayerData[2]
Rank.Value = PlayerData[3]
end
else
warn(ErrorMsg)
end
end)
local function SavePlayerData(Player)
local Player = game.Players.LocalPlayer
local LdrStats = Player:FindFirstChild("leaderstats") -- Error is coming from here
local PlayerData = {LdrStats.Bank.Value, LdrStats.Wallet.Value,
LdrStats.Rank.Value
}
local Success, ErrorMsg = pcall(function()
ds:GetAsync(Player.UserId, PlayerData)
end)
if not Success then
warn(ErrorMsg)
end
end
game.Players.PlayerRemoving:Connect(function(Player)
SavePlayerData()
end)
game:BindToClose(function(Player)
for _, Player in pairs(game.Players:GetPlayers()) do
SavePlayerData(Player)
end
end)