"leaderstats is not a valid member of Player.[whateveryourusernameis]"

Hello I am trying to make an auto-clicker script and seems like I ran into an issue.

auto click script:

local AC = game.ReplicatedStorage.AutoClick
local plr = game.Players.LocalPlayer
local leaderstats = plr:FindFirstChild("leaderstats")
local db = false

while AC do
	if db == false then
		plr.leaderstats.Clicks.Value = plr.leaderstats.Clicks.Value +1
		db = true
		wait(1)
		db = false
	elseif AC == false then
		plr.leaderstats.Clicks.Value += 0
	end
end

leaderstats script:

local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")
local db = false

local function onPlayerJoin(player)  -- Runs when players join
	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local c = Instance.new("IntValue") --Sets up value for leaderstats
	c.Name = "Clicks"
	c.Parent = leaderstats

	local r = Instance.new("IntValue") --Sets up value for leaderstats
	r.Name = "Rebirths"
	r.Parent = leaderstats
	

	local playerUserId = "Player_" .. player.UserId  --Gets player ID
	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	if data then
		c.Value = data['Clicks']
		r.Value = data['Rebirths']
	else
		-- Data store is working, but no current data for this player
		c.Value = 0
		r.Value = 0
	end
end

local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)  --Runs when players exit

	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = "Player_" .. player.UserId
		playerData:SetAsync(playerUserId, player_stats) --Saves player data
	end)

	if not success then
		warn('Could not save data!')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

game.ReplicatedStorage.Click.OnServerEvent:Connect(function(player)
	player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value +1
end)

game.ReplicatedStorage.Sell.Event:Connect(function(player)
	if db == false then
		player.leaderstats.Rebirths.Value = (player.leaderstats.Rebirths.Value + player.leaderstats.Clicks.Value)*1
		player.leaderstats.Clicks.Value = 0
		db = true
		wait(1)
		db = false
	end
end)

I think I might know the problem but im not sure

Is the problem that I am using a local script and I should use a normal script instead?

Change everything to a :WaitForChild, instead of Parent.Child or Parent:FindFirstChild(Childname)
Error is because you have to wait for the script to create the leaderstats before using it

I found the solution its just that I needed to change to a normal script

Oh alright. (Wait leaderstats was a local script? ohhh)

If this is on a LocalScript, you can’t access the DataStore like that

You have to change it to a Server Script inside ServerScriptService

And on your autoclick script:

local plr = game.Players.LocalPlayer
local leaderstats = plr:FindFirstChild("leaderstats")
plr.leaderstats.Clicks.Value = plr.leaderstats.Clicks.Value +1

You’re only changing the stats from the client side, it won’t replicate to the server

Use RemoteEvents to change the leaderstats from the client to the server that way, so that it can properly save the Data, and make sure that API Services are turned on in your Game Settings

the data is fine but its just the autoclicker. thx for contribution jackscarlett its just that I needed to change my auto-clicker local script to a script