Script output "attempt to index nil with 'leaderstats'"

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    A script to add money (Jenny) to the player.
  2. What is the issue?
    Output is giving “attempt to index nil with ‘leaderstats’”
  3. What solutions have you tried so far?
    Changing the script 3 times, googling “attempt to index nil with ‘leaderstats’”.
local Jenny = script.Parent
local Players = game:GetService("Players")
local playerGui = Players.LocalPlayer.PlayerGui
local button = playerGui:WaitForChild("Auction").J1000
local player = Players:GetPlayerFromCharacter(Jenny.Parent)

button.MouseButton1Click:Connect(function()
while wait(0.001) do
		player.leaderstats.Jenny.Value = player.leaderstats.Jenny.Value + 10000
	end
end)
1 Like

Why not do instead:
local player = Players.LocalPlayer

2 Likes

I would suggest you start over and look at the ROBLOX tutorial.

May I ask, are you making an HXH based game? If so let me know that’s my favorite anime.

I already finished the roblox tutorial, and im not making a hxh game only using it as an example.

1 Like

oh Alright.

Well one, wait is deprecated.

Two: You should use a local script for this.

Line 5 in your code is returning Nil, because Jenny.Parent is equal to leaderstats and it cannot compare leaderstats to a players character model because leaderstats is not a players name

I recommend you get the player from Whoever pressed the button.

MouseButton1Click it mean u trying make button click
create a remote event in replicateStorage

Client side (put the script inside starterGui):

local button = script.Parent.Auction.J1000
local player = game.Players.LocalPlayer
local remote = game.ReplicateStorage.RemoteEvent

button.MouseButton1Click:Connect(function()
	while wait(0.001) do
		remote:FireServer()
	end
end)

Server side:

local remote = game.ReplicateStorage.RemoteEvent

remote.OnServerEvent:Connect(function(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    remote.OnServerEvent:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats")
	if leaderstats then
		leaderstats.Jenny.Value += 10000
	end
end)

sugget u watch this seri to learn code (he teach very easy to understand)



Also when using Remote, make sure to add WaitForChild(“RemoteEvent”)

Try using :WaitForChild() for the leaderstats, and also do an if statement on the player: if player then end

I already know how to code the code, its a slap in the face to ask me to watch a video about probbaly properties when all I need to do is look into my notebook to find everything I noted. Furthermore creating an event is harder work than needed to be done. All I need to do is access the value from the folder and do math to add money.
An easy fix would be this.

local Players = game:GetService("Players")
local playerGui = Players.LocalPlayer.PlayerGui
local button = playerGui:WaitForChild("Auction").J1000
local player = game.Players.LocalPlayer

button.MouseButton1Click:Connect(function()
	player.leaderstats.Jenny.Value = player.leaderstats.Jenny.Value + 10000
end)

But couldn’t expect a knuckle head to use local player, maybe you should watch beginner scripting tutorials. Alvin blox is better. Learned how to code in 2 days, today is the 3rd day.

The error “attempt to index nil with ‘leaderstats’” is likely being caused because the ‘player’ variable is not being correctly assigned to the player who triggered the script. You can try changing the line local player = Players:GetPlayerFromCharacter(Jenny.Parent) to local player = Players:GetPlayerFromCharacter(Jenny.Parent.Parent) to ensure that the script is targeting the correct player.

Additionally, the infinite while loop in the button click function will cause the script to continuously add 10,000 to the player’s Jenny value, which could potentially cause performance issues or other problems. You should consider using a one-time connection to the button click event instead of a while loop.

Here’s an updated version of the script that should address these issues:

local Jenny = script.Parent
local Players = game:GetService("Players")
local playerGui = Players.LocalPlayer.PlayerGui
local button = playerGui:WaitForChild("Auction").J1000

button.MouseButton1Click:Connect(function()
	local player = Players:GetPlayerFromCharacter(Jenny.Parent.Parent)
	if player and player.leaderstats then
		player.leaderstats.Jenny.Value = player.leaderstats.Jenny.Value + 10000
	end
end)

This script waits for the button to be clicked once and assigns the correct player to the ‘player’ variable. It also checks if the player’s ‘leaderstats’ folder exists before attempting to access the ‘Jenny’ value.

I recommend using LocalPlayer for LocalScript, since it’s a LocalScript for you, instead of doing Players:GetPlayerFromCharacter(Jenny.Parent), do Players.LocalPlayer, it’s simpler.

Make sure to use task.wait() instead of wait, wait is not accurate at all.

local Players = game:GetService("Players")
local playerGui = Players.LocalPlayer.PlayerGui
local button = playerGui:WaitForChild("Auction").J1000
local player = Players.LocalPlayer

button.MouseButton1Click:Connect(function()
	while task.wait() do
		player.leaderstats.Jenny.Value = player.leaderstats.Jenny.Value + 10000
	end
end)

Since you used Players.LocalPlayer for the Gui, You can replace Players:GetPlayerFromCharacter(Jenny.Parent) with Players.LocalPlayer instead, Also I recommend changing wait() with task.wait() as it works better

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