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

Invalid torso (doesn't get the Vector3 value)?

So I've been working on my game and I've got a script that shows a GUI when I come in 10 studs radius of the NPC or as I named it: "Dummy". The Dummies are located in a special folder called "Dummys" in the workspace. I have been recently working on the "Magnitude" function. I am fairly new to scripting and I am still learning. For the scripts, I want to do I normally get stuck on them for hours upon hours, and I have to watch a lot of videos in order to fix certain sections of the scripts. The local script's location is in a Gui and that Gui is located in StarterGui.

The error message says:

Players.Himillumine.PlayerGui.Every.Checker:16: bad argument #1 to '?' (Vector3 expected, got nil)

This is the script:

	
	local Players = game:GetService("Players")
	local player = Players.LocalPlayer
	local myTorso = player.Character:GetChildren("Torso")
 	local myHumanoid = player.Character:FindFirstChild("Humanoid")
 

	function findTarget()
		local dist = Vector3.new(10, 10, 10)
		local target = nil
		for i,v in pairs(game.Workspace.Dummys:GetChildren()) do
			if v.Name == "Dummy" then
			local human = v:FindFirstChild("Humanoid")
			local torso = v:FindFirstChild("Torso")
			if human and torso then
				if human.Health > 0 then
						if (myTorso.Position - torso.Position).Magnitude < dist then
							dist = (myTorso.Position - torso.Position).Magnitude
							target = torso
							print("Works!")
						end
					end
				end
			end
		end
		return target
	end
	
	while wait(1) do
		local torso = findTarget()
		if torso then
			script.Parent.Enabled = true
			print("YES!")
		else
			print("NOO!")
		end
	end

2 comments

1

player.Character:GetChildren("Torso") isn't a thing. Depending upon when this script runs, you may need WaitForChild() in place of some of your FindFirstChild() calls. Also, for getting the Humanoid, it's preferable to use FindFirstChildOfClass("Humanoid").

0

Thank you so much I appreciate it.

1 answer

✓ Accepted answer

There's a number of reasons why this could be erroring, but I suggest printing both myTorso and torso before the if statement. It's possible you put the wrong index in or one of the two does not exist or were removed. Hope that helps.

1 comment

0

Thank you! I have tried the print command yesterday (i printed both player's and NPC's torso position) and I know that the player's torso is to blame for the error because it didn't print it's position. I don't have a clue about how I could find a local player's torso in the player's character any other way...

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