Roblox errors "attempt to index a nil value" because your v variable is out of scope. "attempt to index a nil value" means you are trying to access a variable's member through the '.' or ':' operators but that variable is nil; it has no value.
About a variable's scope:
When we say a variable is out of scope, we mean the variable's lifetime is over; It does not exist anymore. In this particular example, this is due to the fact that the variables i and v are local to your for loop. In other words, the variables are deleted after the for loop is done executing; at the end of the loop.
There are two kinds of variables in Lua (not to be confused with a variable's type):
Global variables and local variables.
A global variable's scope is that of the whole script. It exists throughout the entire "file".
A local variable's scope is the level of the block in which it is declared in (starting at the line following the one it was declared on). They are introduced by prefixing them with the local keyword.
E.g:
a = 2
if a == 2 then
local b = "test"
if b == "test" then
local c
end
end
local d = 4
function setValueOfE(value)
e = value
end
setValueOfE(2)
print(e)
e = 1
print(e)
do
f()
local function f()
print("Hello, World!")
end
f()
local function factorial(n)
if n < 0 then
error("n is negative", n)
elseif n < 2 then
return 1
else
return n - factorial(n - 1)
end
end
end
f()
g()
local myFunction1 = function()
print("Hello!")
end
local myFunction2 = function()
print("World!")
end
local function execute(someFunction)
someFunction()
end
local function printHelloWorld()
print("Hello, World!")
end
execute(printHelloWorld)
execute(function() print("This works too!") end)
Alright, that's it for the theory. Let's jump to your problem.
There are two solutions to your problem:
Solution #1: Use a global variable
This is -- and any good Lua scripter will agree with me -- the easiest (as in, you won't have to change your script too much) but also ugliest way to solve this problem. While you won't have to rethink your script much, it is considered bad practice. Very bad. Unless you have a really good reason not to, you should always stick to local variables. True, global variables and functions are convenient. Simply by typing a = 5 or using the _G table (_G.a = 5, which is the exact same thing), you can create a variable that’s accessible from anywhere in your code. But there are also multiple reasons why this is bad. First of all, Lua is much more efficient and faster when working with local variables than global variables. Secondly, say I were to write something like this: string = "myString"; In this case I would have completely trashed the string library just because I wanted a variable named 'string'! Whereas if I used a local variable, the string library (which is really just a global table filled with functions) would never have been modified. There are other cons to using global variables or the global table, but I won't discuss them all here. Let's just say they are out of scope of this answer. :)
Solution #2: Subscribing to the Died event
Instead of Wait()ing for a player to die, you could instead subscribe to the Died event of the player characters' Humanoid, here's an example:
function onDeath(character)
print("Someone died")
end
function onPlayerAdded(player)
if v.TeamColor == BrickColor.new("Bright red") then
local character = v.Character
if character then
local humanoid = v:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:connect(onDeath)
end
end
end
end
game.Players.PlayerAdded:connect(onPlayerAdded)
Alright. That was a particularly long read but I hope it helped you and that maybe you've learned a few things. Please be sure to upvote and mark my answer as accepted if it did. If you still need help with this, feel free to leave me a message.
4 comments
Please edit your answer, press the lua button, and place your code inside the two lines.
You should really use a code block.
Yeah I apologize sincerely, I forgot to put it. I have fixed it though, sorry
I don't think you should be using a repeat loop. Also, you're checking if the health value is a string instead of a number value, or an int value. This means that if you do manage to get the repeat to work, it could crash your studio, but it would probably just error.