Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Finding a child of the mouse's target?

Asked by 8 years ago

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)
0
So what is the exact issue, is this th full script. This seems a little broad koolkid8099 705 — 8y
0
I'd include the entire script because people might get mad TheHospitalDev 1134 — 8y
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 Volodymyr2004 293 — 8y

2 answers

Log in to vote
1
Answered by
legosweat 334 Moderation Voter
8 years ago

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!

Ad
Log in to vote
0
Answered by
Kryddan 261 Moderation Voter
8 years ago

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.

Answer this question