Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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)`
0
Is this a local script or a server script Cirillix 110 — 3y
0
local script TimoteoRoblox 10 — 3y

1 answer

Log in to vote
1
Answered by
appxritixn 2235 Moderation Voter Community Moderator
3 years ago

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)
Ad

Answer this question