Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unexpected error in a loop with magnitude?

function Target()
	for i,v in pairs(workspace:GetChildren()) do
		Human = v:FindFirstChild("Humanoid")
		if Human then
			Target = v.Torso

		 print(v.Name)	
		return Target
		end
	end
end



wait(3)
while wait(1) do
	Target = Target(lol)
	Distance = (workspace.Part.Position - Target.Position).Magnitude
	print((workspace.Part.Position - Target.Position).Magnitude)
	if Distance < 10 then
		
	end
end

	Target()

It runs through the loop once, then gives me this error on the second try: 13:09:48.174 - Trying to call method on object of type: Part with incorrect arguments. 13:09:48.175 - Stack Begin 13:09:48.175 - Script 'Workspace.Script', Line 17 13:09:48.176 - Stack End

2 comments

0

your function doesn't have an argument, that's why it's not working

0

I tried with an argument and without. It still won' work.

1 answer

✓ 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 -- overwriting the value

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() -- use an appropriate name
      -- some code ....
end

while true do
	wait(1)

	local target = findTorso()
        local distance  = (workspace.Part.Position - target.Position).Magnitude
	if distance  > 10 then
		-- ect...
	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.

1 comment

0

Thank you so much. This is by far the best answer I have ever seen on this site. I will definitely look over variables. Thank you again.

This discussion is preserved as it was. New answers, comments, and votes are closed.