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

Can't find a fix "attempt to index field "leaderstats" a nil value" ?

Asked by 5 years ago

Basically I want to click on a model that'll then dissapear and give you money yet I get the error saying taht they're trying to attempt the field "leaderstats" a nil value, here is the script

  • [ ]
  • function OnClick()

          local player = game.Players:GetPlayers()
    
          for i=1,#player do
    
          if (player[i]:FindFirstChild("leaderstats") ~= nil) then
    
          local clicks = player[i].leaderstats.Money
    
          if (clicks ~= nil) then
    
          player.leaderstats.Money.Value = player.leaderstats.Money.Value +
          250
    
          script.Parent.Box.CanCollide = false
    
          script.Parent.Box.Transparency = 1
    
          script.Parent.TV3.CanCollide = false --Change COUCH to the name
          of your model!
    
          script.Parent.TV3.Transparency = 1 -- same here
    
          wait(60) -- Change this to the amount of time you want until it
          respawns (can be any number)
    
          script.Parent.Box.CanCollide = true
    
          script.Parent.Box.Transparency = 1
    
          script.Parent.TV3.CanCollide = true -- change COUCH to the name
          of your model !
    
          script.Parent.TV3.Transparency = 0 -- same here
    
          end
    
          end
    
          end
    
          end
    
    
    
          script.Parent.ClickDetector.MouseClick:connect(OnClick)
    
0
line 26, "respawns" is supposed to stay with the green, isn't it? this may be stupid, but try deleting the green text in lines 25-26 just in case any of it is accidentally code and not green ScrubSadmir 200 — 5y
0
^^ Going with this idea too. I would say it is best to remove all the comments and try again. BenjySmb 16 — 5y
0
Nope, wasn't the issue :'( Hamistur 2 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The reason you're getting that error is because the variable "player" is defined as a table of all the players in the server, not just a single player. You already have a variable that is defined as the players money, so you can replace line 13 with:

clicks.Value = clicks.Value + 250

After fixing this I believe that your code rewards every player in the server with money, not just the player who clicked the model which I believe this is not what you want. To fix this you can recreate your function with a "playerWhoClicked" parameter instead. For example:

local function OnClick(player)
    local money = player.leaderstats.Money
    money.Value = money.Value + 250
    local objects = script.Parent:GetChildren()
    for i = 1, #objects do
        local object = objects[i]
        if object:IsA("BasePart") then
            object.Transparency = 1
            object.CanCollide = false
        end
    end
    wait(60)
    for i = 1, #objects do
        local object = objects[i]
        if object:IsA("BasePart") then
            object.Transparency = 0
            object.CanCollide = true
        end
    end
end
script.Parent.ClickDetector.MouseClick:Connect(OnClick)

For other information about ClickDetector.MouseClick, you can visit this page: https://developer.roblox.com/api-reference/event/ClickDetector/MouseClick

0
I'm not very good at explaining, but hope this helps! MegaManSam1 207 — 5y
Ad

Answer this question