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

Finding a child of the mouse's target?

In the game that I'm making I require that when the mouse hovers over something, it checks if it has a certain child and then runs a script. Heres what I tried that didn't work:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Getting the player's mouse

mouse.Move:connect(function() --When the mouse moves
	local target = mouse.Target
	if target:FindFirstChild(game.Workspace.Build.Object.Value) then
		--do this
	end
end)

3 comments

0

So what is the exact issue, is this th full script. This seems a little broad

0

I'd include the entire script because people might get mad

0

Sorry for not making it clear enough. Object is a string value that states the name of what I want to find. Also the only other things in this script is defining target so I didn't think that was necessary

2 answers

✓ Accepted answer

EDIT: So this is a localscript that'll get the player's mouse movements. If the target of the mouse has a child named the same as a StringValue in 'Object' -- Child of Build.

LocalScript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse() --Getting the player's mouse

mouse.Move:connect(function() -- connect the function
    local target = mouse.Target -- Get the mouse target
	if target then -- if the mouse is targeting something, if not, no need to run the script
		local check = target:FindFirstChild(game.Workspace.Build.Object.Value) -- Make sure the Value is a String/Object Value
    	if check then -- Now we see if the target has a child named of that value
        	print("Test") -- Do whatever here
		end
    end
end)

You should get a basic understanding of how to do this!

Hope this helps!

You want to check if the target of your mouse has a certain child, so why are you checking through game.workspace? I don't know what child you want it to look after but if we say it's Object for example then:

if target:FindFirstChild("Object") then

There is no reason to check through the whole workspace because then you're not even checking after the targets children which will not work with FindFirstChild. And Value is not a child of anything, it's a property.

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