Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Error on leaderstats?, "attempt to index nil with 'leaderstats'"

The Error says "attempt to index nil with 'leaderstats'" in the output. Anyone help me because i used both localscript and serverscript.

` local values = {
	100,
	200,
	300,
	400,
	500,
	600,
	700,
	800,
	900,
	1000
}
script.Parent.MouseButton1Click:Connect(function(player)
	local hum = player.leaderstats
	local val = hum.Points
	if val.Value ~= values then

	else
		val.Value = val.Value - 100
		local newcoil = game.ReplicatedFirst.GravityCoil:Clone()
		newcoil.Parent = player.Backpack
	end 
end)`

2 comments

0

Is this a local script or a server script

0

local script

1 answer

Problem

  • You are getting a nil value for player. The reason for this is because the MouseButton1Click event does not provide any parameters.

Solution

  • Assuming it is only the local player pressing the button, you could declare a 'player' variable earlier in your script.

Code

--=== Services ===--

local Players = game:GetService("Players")

--=== Variables ===--

local values = {
    100,
    200,
    300,
    400,
    500,
    600,
    700,
    800,
    900,
    1000
}

local player = Players.LocalPlayer -- Declare it here

--=== Connections & Main Loop ===--

script.Parent.MouseButton1Click:Connect(function() -- remove the argument here
    local hum = player.leaderstats
    local val = hum.Points
    if val.Value ~= values then

    else
        val.Value = val.Value - 100
        local newcoil = game.ReplicatedFirst.GravityCoil:Clone()
        newcoil.Parent = player.Backpack
    end 
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.