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

how do I test what mouse.Target/Hit is?

A sequel to my original question So, I am making a game where something happens when the player looks at a monster(weeping angel,etc) but I can't seem to find the right code. What I want is that when the player's mouse is over the monster, it registers that. here is some very limited sudo code

if mouse target = monster then
print("seen monster")

Edit: so now my code looks like this: Local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
while true do
if mouse.Target:IsDescendantOf(workspace.Monster) then
	    print("pointing at monster")
	end
wait()
end

And now it Edit: I am an idiot. sorry, everyone. I thought I tried the script, but I didn't do it correctly. I apologize. I will try to avoid do that again.

2 comments

0

mouse.Target will be a part or nil, not a model

1

targificate comment incap:)

1 answer

✓ Accepted answer

The main problem here is that using are using the set operator = instead of the equivalence operator == to see if something is equivalent to something else.

A fixed version of this would be :

if mouse.Target:IsDescendantOf(workspace.Monster) then
	print("pointing at monster")
end

A misconceptions that is present in your problem is that a model can be a seen as a target of a mouse, which it can't as the mouse target can only be a base part

Which is why I used the IsDescendantOf function, as body parts are generally descendants of a character / npc.

Hopefully this helped!!

8 comments

0

Seems like it would, but it says mouse is an unknown global. Do you know how to solve that?

0

smh

0

Perfect example of somebody trying to take a Script for free. He didn’t define mouse. This is why you got it, don’t copy and paste

0

I tried to search it up, however the wiki and many tutorials didn't help me. I wasn't trying to take a script, I'm just new. Sorry if I did something wrong. Also, I meant player, but I figured it out anyway.

0

ROBLOX Wiki: Mouse.Target first thing when you search it up

0

I tried that. But now it works? Hold on. your right oh my god i hate myself rn

0

Ok, you have to actually define the mouse variable as the mouse of the LocalPlayer

0

game.Players.LocalPlayer:GetMouse()

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