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

How to trigger something when a mouse is over something?

This is not a request, I want an explanation

How would you trigger something ( make something happen ) when someones mouse is over a part. Links to wiki posts, what it would use,and an explanation of what everything does would be helpful And so would an example, thanks!

2 answers

✓ Accepted answer

This is a bit of a tougher concept. There are however many ways to go at it.

The first way (my personal way) is to use the line mouse.Target.Name, put an if afterwards, and then have it hard coded to check for certain words. From there, you could then set a variable (in lighting, for instance) to a value if the name of the part equals a name in a table or list.

From there, add a repeat wait() until game.Lighting.variable.Value == true or a Changed event to run the designated code.

It will require being in a LocalScript, and will need mouse defined (local mouse = game.Players.LocalPlayer:GetMouse())

Or, if you're not looking for specifics, and just to check if it's a part, you could just do this --

if mouse.Target ~= nil then
end

Now, the second way is a bit more professional, however, it's definitely more complex. That way would be Raycasting. It's great with detecting parts and many other detection sources (best used with a tool of some sort). Here's a good wiki page for general Raycasting, and what looked to be a good Raycasting basics page to help you get started.

The first example in the basics might already be similar to what you're looking for.

I wouldn't fully suggest using these methods without it being a tool or a LocalScript inside of StarterGui, but maybe there's a workaround I'm not thinking of at the moment.

Those are what I believe to be the best ways to check for parts, as both have their strengths and weaknesses. I'm sure there are more, but they're either inferior to both listed above, or I'm not aware of them.

If you have any other questions, feel free to ask, and I'll answer to the best of my knowledge. Good luck in your endeavors!

4 comments

0

Here is my question, since this is a Local Script, would it be possible to have it so when someone hovers their mouse over a part, it modifies a brick inside of Workspace?

0

Most likely, what kind of modification are we talking?

0

A gate opening or a door opening

1

That would work just fine. I'd suggest making a boolean variable, placing it somewhere, and making the "part" script set it to true. The gate could then have a non-local script that listens for when the bool value is either changed or == true.

Use the Changed event:

function onHover(Part)
	print("Mouse hovering above " .. Part.Name);
end

mouse.Changed:connect(function(Property)
	if Property == "Target" and mouse.Target then -- if Target is changed, and it exists
		onHover(mouse.Target);
	end
end);

You can also do it with coroutines, if you like:

getNextHover = coroutine.wrap(function()
	repeat
		repeat until mouse.Changed:wait() == "Target" and mouse.Target;
		coroutine.yield(mouse.Target);
	until false;
end);
while true do
	onHover(getNextHover());
end

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