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)
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.
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)
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