I want to access a specific child of a tool to get its position.
**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
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.
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.
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
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!!!
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.