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

Why am I getting nil?

Asked by 9 years ago

I'm trying to write a script to increase the IntValue inside a player when they kill another player, but the game is returning nil when I search for their name in the Player's tree.

local Humanoid = script.Parent.Humanoid
function killed()
    local creator = script.Parent.Humanoid:FindFirstChild("creator")
    local playerName = creator.Value
    print(playerName)  --Prints Player2 (the player I was using to kill Player1)
    local id = game.Players:FindFirstChild(playerName)  --why is this returning as nil when the player exists?
    print(id) --prints nil
    local elo = id.ELO
    elo.Value = elo.Value - 15
    script:remove()
end
Humanoid.Died:connect(killed)
0
Try replacing "FindFirstChild" with "WaitForChild", on "local id". If that doesn't work, replace all "FindFirstChild" to "WaitForChild". PreciseLogic 271 — 9y

2 answers

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

Problem

It's because the creator value is an ObjectValue, which is the player. Since the output has no other way to print the object value, it presents you with its name. Thus why you're getting the issue.


Solution

You can keep the value at default, and directly access "ELO".

local Humanoid = script.Parent.Humanoid
function killed()
    local creator = script.Parent.Humanoid:FindFirstChild("creator")
    local playerName = creator.Value
    print(playerName)  --Prints Player2 (the player I was using to kill Player1)
    local elo = playerName.ELO
    elo.Value = elo.Value - 15
    script:remove()
end
Humanoid.Died:connect(killed)

Hopefully this answer helped, if so hit the upvote button. If this answered your question then hit accept answer. If you have any questions or comments, post in the comments below.
0
Thanks, this worked! Just curious, could you explain more how the script knows to jump directly to the Players tree when presented with the name? Phantom1996 45 — 9y
0
The script knows to jump directly to the player when the player's object is the value of the ObjectValue. The ObjectValue is not set with the name of the player, but the physical player itself. M39a9am3R 3210 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

The easiest way of doing this is creating a table. Youd then iterate through the table and find the player with the name "player2". The script would look like this:

local Humanoid = script.Parent.Humanoid
function killed()
    local creator = script.Parent.Humanoid:FindFirstChild("creator")
    local playerName = creator.Value
    print(playerName)  --Prints Player2 (the player I was using to kill Player1)
    players = game.Players:GetChildren()    --this creates a table of all the players in - game
        for i,v  in pairs(players) do
            if v.Name == playerName then
            player = v      --this will be the player you indexing
        return player
    end
    print(id) --prints nil
    local elo = id.ELO
    elo.Value = elo.Value - 15
    script:remove()
end
Humanoid.Died:connect(killed)

Here's an explanation. First we create a table. A table is a list of values that are stored in one variable. If you want to find one of the values within the table, we would iterate through the table. This means the lines of code within the "for i,v in pairs" will run. Then, if the value were on is equal to the PlayerName variable, then we index the player using the v. V stands for the value were using. Return, returns the value and allows you to grab it from anywhere in the script.

Hopefully this is a good enough start to your script. Of coarse I really don't expect you to do much with it :/. If you don't understand the concept, I have some links below. If you have any questions, please comment.

Tables: http://wiki.roblox.com/index.php?title=Table An in-depth scripting guide: http://wiki.roblox.com/index.php?title=Scripting_Book

Answer this question