Trying to add to a leaderstat

Hello! I’m trying to add to a leaderstat
‘’‘game.Players:FindFirstChild(PlayerName).leaderstats.Coins += 1’‘’
and it’s saying “attempt to index nil with ‘leaderstat’” note: this is a server script.

1 Like

You cant change the value of a numbervalue just by doing value += #
You need to change the value property, try

game.Players:FindFirstChild(PlayerName).leaderstats.Coins.Value += 1

There’s an error that says: attempt to index nil with ‘leaderstats’

Maybe …

game.Players.PlayerName:FindFirstChild("leaderstats").Coins.Value += 1

Really don’t like to try and cram everything on one line like that … can you show the script?
And how it’s being used …

Can I see the leader stats script?

Here it is

spawn(function()
wait(3)
print(#PartsHit)
if script:FindFirstAncestorWhichIsA(“Model”)then
local PlayerName = script:FindFirstAncestorWhichIsA(“Model”)
game.Players.PlayerName:FindFirstChild(“leaderstats”).Coins.Value += 1
end
end)

Is the name of your player “PlayerName”, because if it’s not then that’s one problem, also can I see the script that creates the leaderstats

'local stat = “Coins” --Change to your stat name
local startamount = 0 --Change to how much points the player will start out with

local DataStore = game:GetService(“DataStoreService”)
local ds = DataStore:GetDataStore(“LeaderStatSave”)

–Don’t worry about the rest of the code, except for line 25.
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new(“Folder”,player)
leader.Name = “leaderstats”
local Coins = Instance.new(“IntValue”,leader)
Coins.Name = stat
Coins.Value = ds:GetAsync(player.UserId) or startamount
ds:SetAsync(player.UserId, Coins.Value)
Coins.Changed:connect(function()
ds:SetAsync(player.UserId, Coins.Value)
end)
end)

game.Players.PlayerRemoving:connect(function(player)
ds:SetAsync(player.UserId, player.leaderstats.Coins.Value) --Change “Points” to the name of your leaderstat.
end)

sorry about the format, I don’t remember how to format code

Well, it’s a free model but that’s fine, try doing this.

Game.Players:FindFirstChild(“Yournamehere”).leaderstats.Coins.Value += 100

would it work for everyone or just me?

Make sure your not trying to add Coins right as the player joins, if you do, the leaderstats folder won’t be created yet, and throw a error.

Depends on what you want, to give it to everyone or 1 player

here is something that will check if every object exists and if not, wait until it exists.

local PlrName = "Bettercat4" -- insert you player name here
while not game.Players:FindFirstChild(PlrName) do
	wait()
end
local Player = game.Players:FindFirstChild(PlrName)
while not Player:FindFirstChild("leaderstats") do
	wait()
end
local Leaderstats = Player:FindFirstChild("leaderstats")
while not Leaderstats:FindFirstChild("Coins") do
	wait()
end
local Coins = Leaderstats:FindFirstChild("Coins")
Coins.Value += 1

note: this will wait infinitely.
if you want a non-infinite wait then this may work:

local Player = game.Workspace.Players:WaitForChild(PlrName)
if Player then
	local Leaderstats = Player:WaitForChild("leaderstats")
	if Leaderstats then
		local Coins = Leaderstats:WaitForChild("Coins")
		if Coins then
			Coins.Value += 1
		end
	end
end

and if you want to have no wait and run the code only if the object exists then this will work.

local Player = game.Workspace.Players:FindFirstChild(PlrName)
if Player then
	local Leaderstats = Player:FindFirstChild("leaderstats")
	if Leaderstats then
		local Coins = Leaderstats:FindFirstChild("Coins")
		if Coins then
			Coins.Value += 1
		end
	end
end

and each of these is in a LocalScript in StarterPlayerScripts in StarterPlayer but might be able to work if located in a normal script on the server.

also here is a script for adding leaderstats and coins value, located in a server script in ServerScriptService

game.Players.PlayerAdded:Connect(function(plr)
	local Stat = Instance.new("Folder")
	Stat.Name = "leaderstats"
	Stat.Parent = plr
	local Coin = Instance.new("NumberValue")
	Coin.Name = "Coins"
	Coin.Parent = Stat
end)

That will just wait infinitely

yes, if the object does not exist.

That error is because the code couldn’t find any member/property named “leaderstats”. Try using :WaitForChild() for debugging purposes or show the script and tell us here.

@Bettercat4 instead of filling it up with while loops just replace FindFirstChild with WaitForChild

if you don’t want an infinite wait, you can just use

local Player = game.Workspace.Players:WaitForChild(PlrName)
if Player then
	local Leaderstats = Player:WaitForChild("leaderstats")
	if Leaderstats then
		local Coins = Leaderstats:WaitForChild("Coins")
		if Coins then
			Coins.Value += 1
		end
	end
end

ye i just put some alternative code with WaitForChild

Why would you add an if statement when WaitForChild will never provide nil when the second argument is empty? Smells like Roblox Assistant.

local max_wait = nil
local x = something:WaitForChild("another_thing", max_wait)

--> x will never be nil unless max_wait is a number

Also, don’t do this, it’s messy and bad practice:

if something then

end

Instead:

if something == true then

end

Or if it’s something that merely exists:

if something ~= nil then

end