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

How to make a hitbox highlight when mouse hovers over?

local destroyMode = script.Parent.DestroyMode
local p = game.Players.LocalPlayer
local pMouse = p:GetMouse()

destroyMode.Changed:Connect(function()
	if destroyMode.Value == true then 
		pMouse.Move:Connect(function()
			if pMouse.Target:FindFirstChild("HitBox") then 
				pMouse.Target:FindFirstChild("HitBox").Transparency = .8
				end
			end)
	end
end)

this is what i have so far, how would i make the hitbox become invisible once the mouse target hits a different object.

1 answer

Hello sentry3, I am here to help you!

When inserting a ClickDetector, we can use it's functions and properties. In this case we will be using MouseHoverEnter and MouseHoverLeave.

So let's begin, shall we?

I am pretty sure getting the Player's mouse that way is out dated, if you really want to grab the player's mouse then you should use UIS. However, to keep our script nice and simple, we'll just be using ClickDetector(s) as they work the same. UIS is much smoother and doesn't place a big cursor in your screen, however; this is better for new scripters.

So, what you want to firstly do is insert a SelectionBox by pressing CTRL-I ( make sure you didn't type a one, but an I ). Double click this asset and it should be in your Workspace. As an example, place this SelectionBox inside of a Dummy Rig. Now if you like, decorate and make changes to your SelectionBox ( thickness, colour, etc ). After this, set it's Visible property to false.

Now insert a ClickDetector inside of your Rig Dummy. Don't edit this at all, unless you know what you're doing. Finally, insert a regular script and let's begin to code!

What we always have to do before we "actually" code is defining variables. We're going to define the Dummy's ClickDetector and name it cd for short. Having variables is cleaner and can be used for later reference.

local cd = script.Parent:WaitForChild("ClickDetector") -- waits for ClickDetector to load in

Now let's insert our two functions.

cd.MouseHoverEnter:Connect(function() -- creates a function and triggers it when the mouse is hovering over the ClickDetector
	
end)

cd.MouseHoverLeave:Connect(function() -- creates a function and triggers it when the mouse has left the ClickDetector
	
end)

As you can see from viewing the script alone we've made some functions, use the silenced quotes to understand.

Now all we have to do is make the SelectionBox visible and invisible with these functions.

local cd = script.Parent:WaitForChild("ClickDetector")

cd.MouseHoverEnter:Connect(function()
	script.Parent:WaitForChild("SelectionBox").Visible = true -- visible is set to true
end)

cd.MouseHoverLeave:Connect(function()
	script.Parent:WaitForChild("SelectionBox").Visible = false -- visible is set to false
end)

Ta-dah, we have made our script, now if you want the hitbox to be on a player then create a script for whenever a player is added and then make it that it Instance.new's the ClickDetector and SelectionBox into the player!

If you have learnt anything from me, please Accept Answer!

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