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)
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
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')
local targetbox = Instance.new('SelectionBox', 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
targetbox.Adornee = target
else
targetbox.Adornee = nil
end
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
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. :)
PlayerGui - Do I need explain? ;)
Hope this helped you in any way! :D