Can't print Instance results

I am actually doing a Interaction system That when your mouse target is on something like a Part and you press E the RemoteEvent of the part Fires, but i got into this problem that it doesn’t prints the results

Local Script 1 :

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

repeat wait() until _G.CustomMouse
local customMouse = _G.CustomMouse

uis.InputBegan:Connect(function(inputObject, gameProcessEvent)	
	if gameProcessEvent then return end

		if inputObject.KeyCode == Enum.KeyCode.E then
		local hit,pos = customMouse.getTarget(30,{player.Character},true) --This will look for the target of the mouse that is within 30 studs of the camera, ignores the player's character and ignores any parts with a transparency of 1
			if not hit or not pos then return end
		
		
		for i, v in ipairs(hit:GetDescendants()) do
			print(v) -- Print instances (this one works)
			if v:FindFirstChild("_Interactable") then  -- (_Interactable is a Object Value just to confirm that the instance can be interacted, but this doesnt works)
					local remote = v:WaitForChild("RemoteInteractable")
					print("Target Name:"..hit.Name)
			end
		end
	end
end)

CustomMouse Local Script :

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local class = {}

--[[ Parameters:
		distance (number): the distance from the camera that the method will look for a part. Leave as nil to have no set distance.
		ignore (table): the list of objects (descendants of objects in this list as well) that will be ignored.
		ignoreInvisible (boolean): if true, when there is a part with transparency of 1 in the path of the mouse's target, it will be added to the ignore list.
		ignoreCantCollide (boolean): if true, when there is a part with cancollide set to false in the path of the mouse's target, it will be added to the ignore list.
]]

class.getTarget = function(distance,ignore,ignoreInvisible,ignoreCantCollide)
	if not mouse.Hit then return end
	ignore = ignore or {}
	local function scan()
		local ray = Ray.new(camera.CoordinateFrame.p,(mouse.Hit.p - camera.CoordinateFrame.p).unit*(distance or 900))
		if ignore[1] then print("ignore:",ignore[1]) end
		return workspace:FindPartOnRayWithIgnoreList(ray,ignore)
	end
	local complete = false
	local hit,pos = scan()
	repeat
		if not hit or not pos then return end
		if (ignoreInvisible and hit.Transparency == 1) or (ignoreCantCollide and not hit.CanCollide) then
			table.insert(ignore,hit)
			hit,pos = scan()
		else
			complete = true
		end
	until complete
	for i,part in pairs(ignore) do
		print(part.Name)
	end
	return hit,pos
end

_G.CustomMouse = class





1 Like

Try this modified code and see what it warns:

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

repeat wait() until _G.CustomMouse
local customMouse = _G.CustomMouse

uis.InputBegan:Connect(function(inputObject, gameProcessEvent)	
	if gameProcessEvent then return end

		if inputObject.KeyCode == Enum.KeyCode.E then
		local hit,pos = customMouse.getTarget(30,{player.Character},true) --This will look for the target of the mouse that is within 30 studs of the camera, ignores the player's character and ignores any parts with a transparency of 1
			if not hit or not pos then return end
		
		
		for i, v in ipairs(hit:GetDescendants()) do
			print(v) -- Print instances (this one works)
      for _, O in ipairs(v:GetDescendants()) do
        warn(O.Name, O.ClassName)
      end
			if v:FindFirstChild("_Interactable") then  -- (_Interactable is a Object Value just to confirm that the instance can be interacted, but this doesnt works)
					local remote = v:WaitForChild("RemoteInteractable")
					print("Target Name:"..hit.Name)
			end
		end
	end
end)

Debug from there or I can attempt to assist you further.

image
That’s all the things it prints when i Press E, as it wont print the Instance Name Called “Part”

Ok so let’s try this code:

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

repeat wait() until _G.CustomMouse
local customMouse = _G.CustomMouse

uis.InputBegan:Connect(function(inputObject, gameProcessEvent)
    if gameProcessEvent then return end

    if inputObject.KeyCode == Enum.KeyCode.E then
        local hit, pos = customMouse.getTarget(30, {player.Character}, true) -- This will look for the target of the mouse that is within 30 studs of the camera, ignores the player's character and ignores any parts with a transparency of 1
        if not hit or not pos then return end

        for i, v in ipairs(hit:GetDescendants()) do
            print(v) -- Print instances (this one works)
            local Children = {}
            for _, O in ipairs(v:GetChildren()) do
                table.insert(Children, O.Name)
            end
            if table.find(Children, "_Interactable") then -- (_Interactable is a Object Value just to confirm that the instance can be interacted, but this doesnt works)
                local remote = v:WaitForChild("RemoteInteractable")
                print("Target Name:" .. hit.Name)
            end
        end
    end
end)

Not the prettiest nor is it how it “should” be done, but if it works that means there’s something wrong with:
A. Your naming convention containing an underscore,
or
B. I don’t know, should probably report it as a bug.

1 Like

Didn’t worked Sadly, thanks anyways

I’ve got one more idea for debugging. See what all this prints with the prefix "Hit: "

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

repeat wait() until _G.CustomMouse
local customMouse = _G.CustomMouse

uis.InputBegan:Connect(function(inputObject, gameProcessEvent)
    if gameProcessEvent then return end

    if inputObject.KeyCode == Enum.KeyCode.E then
        local hit, pos = customMouse.getTarget(30, {player.Character}, true) -- This will look for the target of the mouse that is within 30 studs of the camera, ignores the player's character and ignores any parts with a transparency of 1
        if not hit or not pos then return end

        for i, v in ipairs(hit:GetDescendants()) do
            print("Hit:", v.Name.." | "..tostring(v)) -- Print instances (this one works)
            local Children = {}
            for _, O in ipairs(v:GetChildren()) do
                table.insert(Children, O.Name)
            end
            if table.find(Children, "_Interactable") then -- (_Interactable is a Object Value just to confirm that the instance can be interacted, but this doesnt works)
                local remote = v:WaitForChild("RemoteInteractable")
                print("Target Name:" .. hit.Name)
            end
        end
    end
end)

Edit: my bad, try this instead @NameSpookbtw


This error prints

I think I know what the error may be, by the way.

You’re going through “hit:GetDescendants()” rather than just checking “hit” itself. Let me update your code and I’ll edit this reply with it.

local camera = workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local uis = game:GetService("UserInputService")

repeat wait() until _G.CustomMouse
local customMouse = _G.CustomMouse

uis.InputBegan:Connect(function(inputObject, gameProcessEvent)
    if gameProcessEvent then return end

    if inputObject.KeyCode == Enum.KeyCode.E then
        local hit, pos = customMouse.getTarget(30, {player.Character}, true) -- This will look for the target of the mouse that is within 30 studs of the camera, ignores the player's character and ignores any parts with a transparency of 1
        if not hit or not pos then return end

        print("Hit:", hit.Name .. " | " .. tostring(hit)) -- Print instances (this one works)
        if hit:FindFirstChild("_Interactable") then -- (_Interactable is a Object Value just to confirm that the instance can be interacted, but this doesnt works)
            local remote = hit:WaitForChild("RemoteInteractable")
            print("Target Name:" .. hit.Name)
        end
    end
end)

Edit: try that.

1 Like

No way, this worked
image

But anyways, I used GetDescendants Because i want to check in all Childrens of the part

Figured it would, since you’re returning the actual part that your mouse points at it should just be the “hit” result.

1 Like