Why can't i access child of tool to get its position? (i feel like i am going insane)

  1. What do you want to achieve?

I want to access a specific child of a tool to get its position.

  1. **What is the issue?

It keeps indexing nil and I dont know why. is this another weird tool thing or can this be fixed. please tell me it can be fixed. why are so many things with tools so absurdly difficult (WHY ARE THEY SO TERRIBLE). i feel like im going insane. please would anyone look at the code and tell me what is wrong

  1. What solutions have you tried so far?

waitforchild, findfirstchild. it can find the tool. it cannot find the mouse.

instrument.Mouse ← another alternative try
player.Backpack[ā€œThe Instrumentā€].Mouse ← another alternative try
instrument:FindFirstChildOfClass(ā€œModelā€) ← another alternative try


local Players = game:GetService("Players")

local function findNearestMousey(pos)

	for _, player in ipairs(Players:GetPlayers()) do -- Loops through the Players Service which contains every player Instance in the game
		local target = nil
		local Character = player.Character or player.CharacterAdded:Wait()
		local instrument = Character:FindFirstChild("The Instrument")
		local mouse = instrument:FindFirstChild("Mouse")
		print(mouse)
		local humanoid = Character:findFirstChild("Humanoid")
		if (instrument ~= nil) and (humanoid ~= nil) and (humanoid.Health > 0) then
			target = mouse
			end
		
		return target
		
	end
end

Just a glance I haven’t looked deeply into it, but try changing the name ā€œMouseā€ to something else. I’ll look what else it could be in a moment.

1 Like

Thank you for your reply. I’ve tried changing it but this does not seem to work. I wonder whether this is a bug? but that cannot be true right

Looking in deeper let’s go through the following possibilities:
Check to see if Instrument is exactly what you’re trying to find by using print(instrument:GetFullName()) right above mouse.

If the instrument is correct, go in studio and check if the mouse deletes itself. If it does then obviously it cannot be found.

Let me know the result.

1 Like

print(instrument:GetFullName()) 
		--local mouse = instrument:FindFirstChildOfClass("Model")
		--local mouse = instrument:FindFirstChild("target")
		print(instrument)

ok…so if I do the first print, its nil and errors. if i do the print i normally use below that, its nil once, doesnt error, and then next times the function is called (in while true do loop) it does print ā€˜the instrument’

see below for loop:


while true do
	wait(0.1)
	local target = findNearestMousey(script.Parent.Torso.Position)
	if target ~= nil then
		script.Parent.Humanoid:MoveTo(target.Position, target)
	end
end

I guess there is something wrong with findin the original tool but…i dont know what
edit: OH. so the first time that instrument is nil it doesnt error, but finding its child does error. for some reason. but why is the first time nil…
edit: i set the wait in the loop to 1 sec but this didnt help

Mouse is trying to reference Instrument which may not exist since that’s what you seem to be searching for. I’m not sure what your print returned but let’s just do it like this and see what happens.

local function findNearestMousey(pos)
	for _, player in pairs(Players:GetPlayers()) do
		local target = nil
		local Character = player.Character or player.CharacterAdded:Wait() -- sure ig
		if Character then
			local instrument = Character:FindFirstChild("The Instrument") -- also... sure...
			if instrument then
				print(instrument:GetFullName())
				local mouse = instrument:FindFirstChild("Mouse")
				local humanoid = Character:findFirstChild("Humanoid")
				print(mouse)
				if mouse and (humanoid ~= nil) and (humanoid.Health > 0) then
					target = mouse
				end
			end
		end
		return target
	end
end
1 Like

THAT FIXED IT. if statement to check whether it is what its supposed to be…doesnt matter what the cause is of the first nil because the loop goes again anyway. of course. i will remember this. thank you so much!!!

edit: I also figured out my noob question about the position of the thing. I just got the part.position. THANK YOU SO MUCH ONCE AGAIN!!!

1 Like

Are there any errors in the console and does printing provide any additional context? Also I don’t think you need the additional ā€œtargetā€ with MoveTo.

script.Parent.Humanoid:MoveTo(target.Position, target) can just be
script.Parent.Humanoid:MoveTo(target.Position) (I’m pretty sure.)

edit: Glad you figured it out. If it works it works. Good luck.

1 Like

Thank you so much you have been extremely helpful more than anyone else ever (this seems like hyperbole but it is not). so thank you, like seriously.

Have a great life!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.