0
votes

I tried to make a script where if I write in a TextBox a name of a player that is in the server he/she will die, but it gives me this error:

attempt to index local 'textBox' (a nil value)

Here's my script:

local screenGui = script.Parent
local textBox = screenGui:FindFirstChild("TextBox", true)

textBox.FocusLost:Connect(function(enterPressed)
  --[[This function is called every time the TextBox is "unfocused". A TextBox is "focused" when the player is typing in it, and "unfocused" when they're doing something else.]]
  --[[a parameter is passed to this function, "enterPressed". This is true if the player unfocused the TextBox by pressing enter. Another way to unfocus it is by clicking somewhere outside it.]]

  --Try to find a player with the name of whatever is in the TextBox
  local player = game.Players:FindFirstChild(textBox.Text)
  if player then --Check if we found that player
    local character = player.Character
    local humanoid = character:FindFirstChild("Humanoid") --try to find the humanoid

    if humanoid then --check if we found that humanoid
      humanoid.Health = 0 --kill the humanoid
    end
  end
end)
1
maybe, instead of textBox use TextBox?Mike V.

1 Answers

3
votes

attempt to index local 'textBox' (a nil value)

This error message tells you that you're indexing a nil value named textBox.

So go to the first line where you index textBox, which is this:

textBox.FocusLost:Connect(function(enterPressed)

Why is textBox nil in this line? Well let's see where we asign a value to textBox prior to that line.

local textBox = screenGui:FindFirstChild("TextBox", true)

So obviously screenGui:FindFirstChild returns nil.

Refer to the reference manual http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild&redirect=no

Here we read:

Returns the first child found with the given name, or nil if no such child exists. If the optional recursive argument is true, recursively descends the hierarchy while searching rather than only searching the immediate object.

So according to the reference manual you don't have a child named "TextBlock" so you cannot find one.

So first of all, if this can be nil you might want to make sure textBox isn't nil befor you index it.

if textBox then
  textBox.someFancyIndex()
end

You already did this for humanoid btw:

local humanoid = character:FindFirstChild("Humanoid") --try to find the humanoid

if humanoid then --check if we found that humanoid
  humanoid.Health = 0 --kill the humanoid
end

So why not for textBox?

Then of course you have to find out why you don't have an object named "TextBox". Are you looking in the wrong instance? Did you forget to create that object?

That's someting we cannot tell you as you did not provide the respective code.