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:
1 | local player = game.Players.LocalPlayer |
2 | local mouse = player:GetMouse() --Getting the player's mouse |
3 |
4 | mouse.Move:connect( function () --When the mouse moves |
5 | local target = mouse.Target |
6 | if target:FindFirstChild(game.Workspace.Build.Object.Value) then |
7 | --do this |
8 | end |
9 | end ) |
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
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() --Getting the player's mouse |
03 |
04 | mouse.Move:connect( function () -- connect the function |
05 | local target = mouse.Target -- Get the mouse target |
06 | if target then -- if the mouse is targeting something, if not, no need to run the script |
07 | local check = target:FindFirstChild(game.Workspace.Build.Object.Value) -- Make sure the Value is a String/Object Value |
08 | if check then -- Now we see if the target has a child named of that value |
09 | print ( "Test" ) -- Do whatever here |
10 | end |
11 | end |
12 | 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.