Datastore isn't working for some reason

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)
1 Like
game.Players.PlayerRemoving:Connect(function(Player)
    SavePlayerData(Player) -- you pass player as an argument
end)

This should fix it, you called the function SavePlayerData without passing the Player

Nope, It’s giving me this error now.
ServerScriptService.Ldrstats:48: attempt to index nil with 'FindFirstChild'

oh lol. you tried to use game.Players.LocalPlayer in a server script.

local Player = game.Players.LocalPlayer -- delete this line

Now I’m getting no error. So I joined my game, modified my stats, rejoined, and it was gone. Very confused why it isn’t working.

That is because you use GetAsync in the SaveData function instead of SetAsync.
I would recommend you stop watching tutorials that give off pre made scripts and read the documentation of Data Stores

Works! Thank you, also for the tutorials it isn’t entirely from the tutorial, some was made by me. But yeah, thanks for providing the documentation! I’ll look there next time.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.