✓ Accepted answer
You should go back and look at how variables should work. You are creating global variables which is not good practice.
Your error is due to like 17 which will be overwriting the global variable with nil or v.Torso.
Target = Target(lol)
The way you use the function also shows that you need to review how you use variables. On line 5 you have the same problem. In addition you are returing the variable Target which is a global variable.
Target = v.Torso
Firstly you need to be using local variable in your work. They are accessed faster and limited by the scope they are created in.
local function Target()
for i,v in pairs(workspace:GetChildren()) do
local Human = v:FindFirstChild("Humanoid")
if Human then
local Target = v.Torso
print(v.Name)
return Target
end
end
end
wait(3)
while wait(1) do
local Target = Target(lol)
local Distance = (workspace.Part.Position - Target.Position).Magnitude
print((workspace.Part.Position - Target.Position).Magnitude)
if Distance < 10 then
end
end
This should be enough to resolve this as Lua will uses the variable name in the closest scope.
Example
local a = "b"
do
local a = "a"
print(a)
end
print(a)
But this is not good practice as it leads to confussion when reading the code. You should name variables and functions correctly.
local findTorso()
end
while true do
wait(1)
local target = findTorso()
local distance = (workspace.Part.Position - target.Position).Magnitude
if distance > 10 then
end
end
Notes
- I am not sure why you have
Target(lol) as your function does not take any arguments.
- The function call at the bottom line 25 will never run
Target() it is blocked by the inf while loop.
I hope this helps please comment if you have any other questions.
2 comments
your function doesn't have an argument, that's why it's not working
I tried with an argument and without. It still won' work.