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

Leaderboard problems: FindFirstChild a nil value...????

Asked by 9 years ago

Hello. So I have a mainscript, and within that a function that looks like this:

function awardcredit(player, credits)
if player and credits then
local leaderstats = player:FindFirstChild("leaderstats") -- line 32 btw
if leaderstats then
local value = leaderstats:FindFirstChild("BlitzCreditz")
if value then
value.Value = value.Value + credits
end
end
end
end

This is supposed to add some credits (A value on the leaderboard. More on that in a moment...) This is where I call this function in the script

awardcredit(activecontestants, 4)

I also have my leaderboard set up like this...

function playeradded(player)
    local leaderstats = Instance.new("Model")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local points = Instance.new("IntValue")
    points.Name = "Wins"
    points.Parent = leaderstats
    points.Value = 0

    local BC = Instance.new("IntValue")
    BC.Name = "BlitzCreditz"
    BC.Parent = leaderstats
    BC.Value = 0



end
game.Players.PlayerAdded:connect(playeradded)
for _, player in pairs(game.Players:GetPlayers()) do
    playeradded(player)
end 

The output says this when I get to the awardcredit(activecontestants, 4) in the script... workspace.MainScript:32: attempt to call method 'FindFirstChild (a nil value) Any idea why?

0
Can you show you declaration of 'activecontestants'? DigitalVeer 1473 — 9y

2 answers

Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

I assume that activecontestants is a table, and that the players referenced inside of the table are actual player objects and not just their Names. If yes, then you cannot simply award the four points to the table activeconstestants * because the *parameter of function * awardcredit* expects a player instead.

Your error simply means that FindFirstChild is not a valid member of the argument passed in as player -- which makes sense because it's a table, and not an object.


You could solve your issue by doing looping through the table when the function is called to award the players individually,like so...

function awardcredit(players, credits)
    if type(players) == type({}) then

        for i = 1, #players do
            if players[i]:FindFirstChild('leaderstats') then
                if  players[i].leaderstats:FindFirstChild('BlitzCreditz') then 
                    local stat = players[i].leaderstats.BlitzCreditz 
                    stat.Value = stat.Value + tonumber(credits) 
                end
            end
        end
    end
end
0
Thank you! Yes, activecontestants is a table. I figured it would just automatically go through the member of the table. Antharaziia 75 — 9y
0
Yeah, no.. and you're welcome. ImageLabel 1541 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

FindFirstChild

Although I am not exactly sure as to what activecontestants has been initialized as, the error you are getting often is from the mistake of calling FindFirstChild on an array. The FindFirstChild method can only be used on instances. Arrays, although they are technically objects, arrays are not instances. You may be slightly confounded on why I am repeating array again and again, although you haven't initialized an array anywhere. The main reason for this is that I'm assuming [since you haven't shown it] that activecontestants was the product of using GetChildren or GetPlayers on a prior instance

For example:

local activecontestants = game.Players:GetPlayers()
print(activecontestants:FindFirstChild("Player1"))
--Error

Again, the main explanation for this is the fact that using GetChildren or GetPlayers returns an array, which FindFirstChild can NOT be used on.


Using FindFirstChild Properly

The proper way to use FindFirstChild is to use on the instance instead. For example, let's make a script that looks for a player 'Player1' inside the Players service.

local plr = game.Players:FindFirstChild("Player1")
if plr then
print(plr.Name .. " is in the Server!")
end

This is how FindFirstChild should be used. Here is an example of what NOT to do:

local plr = game.Players:GetChildren():FindFirstChild("Player1")
if plr then
print(plr.Name .. " is in the Server!")
end

Fixing The Issue

Using what has been discussed, let's try fixing your code. Again, I'm under the assumption that players and activecontestants are both meant to be arrays.

function awardcredit(activePlayers, credits)
for _,v in pairs(activePlayers) do
  local stats = v:FindFirstChild("leaderstats")
  local blitz = v:FindFirstChild("BlitzCreditz")
   if stats and blitz then
      blitz.Value = blitz.Value + credits
   emd
end


Useful Links

FindFirstChild: http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild

GetChildren: http://wiki.roblox.com/index.php?title=API:Class/Instance/GetChildren

Instance Type: http://wiki.roblox.com/index.php?title=API:Class/Instance

Handling Arrays: http://wiki.roblox.com/index.php?title=Table#Reading_from_and_writing_to_arrays

Answer this question