Attempt to index nil with 'leaderstats'?

I was making a game where you need to buy levels and while writing the script there was a problem attempt to index nil with ‘leaderstats’

Script:
local player = game:GetService(“Players”).LocalPlayer
function Hi()
if player.leaderstats.Coin.Value >= 5 then
player.leaderstats.Coin.Value = player.leaderstats.Coin.Value - 5
script.Parent:Destroy()
end
end

script.Parent.MouseButton1Click:Connect(Hi)

1 Like

Why are you changing leaderstat value with localscript?

1 Like

I am not, I don’t use localscript

LocalPlayer is used in LocalScripts only. That might be your issue here.

Possible Solution:

local player = game:GetService("Players"):GetChildren(plr)

function Hi()
    if plr.leaderstats.Coin.Value >= 5 then
        plr.leaderstats.Coin.Value = plr.leaderstats.Coin.Value - 5
        script.Parent:Destroy()
    end
end

script.Parent.MouseButton1Down:Connect(Hi)

That won’t work. On line one where to you get “plr” from?

Can I ask if you’re using UI or not?

Yeah, I am using. And why do you need it?

If you are using player gui then you should use LocalScripts and remote events instead

1 Like