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

Removing Selection when mouse is not hovering over object anymore?

=Script= This script adds a selection box to the object that the mouse is hovering over if it has a value in it

=Problem= I can't figure out a way to remove the selection box once i change targets

=Script=

local	player = game.Players.LocalPlayer
local mouse = player:GetMouse()
	
	mouse.Move:connect(function()
	local target = mouse.Target
	if target ~= nil and target.Parent:FindFirstChild('Interact') and not target:FindFirstChild('Selection') then
			
			targetbox = Instance.new('SelectionBox',workspace)
			targetbox.Name = 'Selection'
			targetbox.Adornee = target
			targetbox.Parent = target
		
	else

	end	
	end)

2 answers

✓ Accepted answer

There're many ways to accomplish this; however, I'm going to do the simple way. DUN DUN DUUUUUUU~N! :O

Before I begin, I'm going to re-indent your code, so that it's tabbed correction:

local  player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:connect(function()
	local target = mouse.Target
	if target ~= nil and target.Parent:FindFirstChild('Interact') and not target:FindFirstChild('Selection') then
		targetbox = Instance.new('SelectionBox',workspace)
		targetbox.Name = 'Selection'
		targetbox.Adornee = target
		targetbox.Parent = target
	else
	
	end
end)

Ahhhh, now it looks much, much better now. :)

Now, we can begin! >:D

First off, lets move the targetbox, lines 7-8, outside the function; however, they're going to before the function is set up, just-in-case if the mouse moves before they spawn:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local targetbox = Instance.new('SelectionBox', game.Workspace) -- I made 'targetbox' a local value, b/c it's not going to be re-created so many times, right? :) I also change workspace to game.Workspace; although, if you're worried about that, you could use game:GetService('Workspace'), which is workspace minus the keyword :P
targetbox.Name = 'Selection'


mouse.Move:connect(function()
	local target = mouse.Target
	if target ~= nil and target.Parent:FindFirstChild('Interact') and not target:FindFirstChild('Selection') then
		targetbox.Adornee = target
		targetbox.Parent = target
	else
	
	end
end)

Now we're talking! :D

Second off, lets add onto (now) line 13 so that when the player's mouse isn't hovering over the specific child, the Selectbox (AKA targetbox) disappears! :O Dr. Strange is here!

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local targetbox = Instance.new('SelectionBox', game.Workspace)
targetbox.Name = 'Selection'


mouse.Move:connect(function()
	local target = mouse.Target
	if target ~= nil and target.Parent:FindFirstChild('Interact') and not target:FindFirstChild('Selection') then
		targetbox.Adornee = target
		targetbox.Parent = target
	else
		targetbox.Adornee = nil -- Lets not destroy it, other if we hover over another child, the code will stop execution & break the script, b/c then the selectionbox would be locked/ destroyed ;-;
		targetbox.Parent = nil
	end
end)

There we go now!! (:D) Lookin' good... ;)

(Optional) Third off, what if we don't want other to see what you're hovering over? ;-; No problem, that can be done by parenting the SelectionBox's parent into the Player's PlayerGui! :O

But doesn't PlayerGui only support GUIs?

From what I remember, I've seen some scripts actually work this way when they put the SelectionBox in their PlayerGui. ;P Let's apply it to line 3, remove line 11 & 14, and use the WaitForChild function to have the code yield until the PlayerGui spawns (or exists w/in the parent), otherwise what would happen if it didn't spawn in time?! ;O

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local playerGui = player:WaitForChild('PlayerGui') -- Created a separate variable for this, b/c my perfectionism kicked in T-T
local targetbox = Instance.new('SelectionBox', PlayerGui) -- Now the SelectionBox's parent will be the player's PlayerGui
targetbox.Name = 'Selection'


mouse.Move:connect(function()
	local target = mouse.Target
	if target and target.Parent:FindFirstChild('Interact') and not target:FindFirstChild('Selection') then -- Removed '~= nil' from here, b/c it's not necessary; they if statement will check if the value is true or not (nil or not)
		targetbox.Adornee = target
	else
		targetbox.Adornee = nil
	end -- Removed the parent-setting codes here, b/c we no longer need them, and it would defeat the purpose of us putting the SelectionBox into the player's PlayerGui XP
end)

Aaaaaand, wa-la! Now it looks much, much better! ;D + It's easier to read, cleaner, & MORE AWESOME!! >:D (Ok, maybe not that; perhaps, cool..? Idk. lol) Lets go grab a burger now, and you're paying man. :D (Or I'll pay, if you don't have enough... #RIPWallet)

Stuff touched on, but didn't go into great detail about

  1. WaitForChild - Will yield the code until a child w/ the same name as the argument (WaitForChild(ARGUMENT)) spawns (or exists) w/in the parent (PARENT:WaitForChild); think of it as pausing the code, or execution of the code, until the child spawns. :)

  2. PlayerGui - Do I need explain? ;)

Hope this helped you in any way! :D

On line 14, set the adornee to nil.

targetbox.Adornee = nil

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