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

Why does it keep saying that my value is nil? how can i fix that?

Asked by 4 years ago
Edited 4 years ago
01local players = game:GetService("Players")
02 
03local Humanoid = script.Parent.Humanoid
04function Died()
05    wait(0.2)
06    local tag = Humanoid:WaitForChild("creator")
07    if tag ~= nil and tag.Value ~= nil then
08        local plr = players:GetChildren(tag.Value)
09        if plr ~= nil then
10            local leaderstats = plr:GetChildren("leadertats")
11            local cash = plr.leaderstats.Cash
12            local kills = plr.leaderstats.Kills
13            cash.Value = cash.Value + 0
14            kills.Value = kills.Value + 1
15            wait(0.1)
16            script:remove()
17        end
18    end
19end
20Humanoid.Died:connect(Died)

ERROR : 10: attempt to call a nil value

Thanks for the help! :)

3 answers

Log in to vote
0
Answered by 4 years ago

'GetChildren' doesn't accept any values. Consider using 'FindFirstChild' for it.

Ad
Log in to vote
0
Answered by 4 years ago

First off, GetChildren returns a table of all the children of the object, it won't find your value. Best thing for this would to be to use FindFirstChild or Waitforchild and then define which child you want to get. Also, I'm guessing you want to get the player not the service 'Players'. If you want to access the player through a script the you should use the PlayerAdded event and save the player who joined to a variable (in the first parameter). If it's a local script the you should use game.Players.LocalPlayer. Also Remove is deprecated, use destroy instead. Sorry if its unclear I'm on mobile.

Hope that helped!

JeffTheEpicRobloxian

Log in to vote
0
Answered by 4 years ago

:GetChildren() as stated above by Jeff returns a table of all the children inside of what you chose, if you wanted to get everything inside the character named 'leaderstats' (presuming you have more than 1), you could create a variable for the children of the character, then loop through it and then do whatever to each individual one.

1local children = plr:GetChildren()
2for _, child in pairs(children) do
3    if child.Name == "leaderstats" then
4        --do stuff
5    end
6end

But I'm 99% sure there's only going to be one individual folder named leaderstats and it was just some misuse of getchildren, try using:

1local leaderstats = plr.leaderstats

OR

1local leaderstats = plr:FindFirstChild("leaderstats")

OR

1local leaderstats = plr:WaitForChild("leaderstats")

Answer this question