How to make hover selectionbox that only shows for one player

I’m making a vending machine and whenever you hover over the buttons on the vending machine a selection box appears. But I have one small problem, it shows for all players, I need to make it so it is only visible to the player that is hovering their mouse over the button.

Script inside of ClickDetector:

function mouseEnter()
	--make selectionbox visible
end

function mouseLeave()
	--make selectionbox disappear
end

script.Parent.MouseHoverEnter:Connect(mouseEnter)
script.Parent.MouseHoverLeave:Connect(mouseLeave)

What it looks like to the player:

What it looks like to the other player:
Screenshot (4)

Sorry for the low quality, I had to resize the photo so you can see it better

Hopefully someone replies because nobody replies to my posts that i need help with

1 Like

It’s very easy, all you have to do is add the selection box in a local, not server script

But then the script wouldn’t work at all.

Why not, all you need is a Local Script in StarterPlayerScripts and change the paths to the button, the only thing that should be done in that script is add the selection box, everything else should be done in the other, normal script

Can you give a better explanation? sorry its just that I didn’t catch most of that, could you show me an image or video so I can better understand it? Sorry if I’m wasting your time.

I can’t because I’m in mobile but I can give you a code example:

local Button1 = <PathToButtonHere>
local Button2 = <PathToOtherButton>

function mouseEnter(button)
	--make selectionbox visible
    local box = Instance.new("SelectionBox")
    box.Adornee = button
    box.Parent = button
end

function mouseLeave(button)
	--make selectionbox disappear
    local selection = button:FindFirstChildWhichIsA("SelectionBox")
    if selection then selection:Destroy() end
end

Button1.MouseHoverEnter:Connect(function()
    mouseEnter(Button1)
end)
Button1.MouseHoverLeave:Connect(function()
    mouseLeave(Button1)
end)

Button2.MouseHoverEnter:Connect(function()
    mouseEnter(Button2)
end)
Button2.MouseHoverLeave:Connect(function()
    mouseLeave(Button2)
end)

You can have a remote event that you fire on the server that tells the client to create or destroy a selection box:

local Remote = path.to.remote

local function mouseEnter(player)
	Remote:FireClient(player, true, script.Parent)
end

local function mouseLeave(player)
	Remote:FireClient(player, false, script.Parent)
end

script.Parent.MouseHoverEnter:Connect(mouseEnter)
script.Parent.MouseHoverLeave:Connect(mouseLeave)

You can create a local script in starter player scripts which creates the selection box or destroys it on the client, so it only shows up for that player.

local Remote = path.to.remote
local Buttons = {}

Remote.OnClientEvent:Connect(function(Enabled, Button)
	--Create the selection box
	if Enabled then
		local SelectionBox = Instance.new("SelectionBox")
		-- Properties
		SelectionBox.Parent = Button
		--Add the selection box to a table which will store it
		Buttons[Button] = SelectionBox
	elseif Buttons[Button] ~= nil and Enabled == false then
		--The button has a selection box and the server wants to destroy it
		Buttons[Button]:Destroy()
	end
end)
1 Like

Good idea, would work, but seems a bit overkill don’t you think.

If he’s connecting to these events on the server, which he is because he’s using a click detector, he needs to send the information to a remote event to have the action happen on the server.

Is this a script inside the clickdetector? Or a local script

This is a script inside of the click detector.

No selectionbox pops up at all.

Code inside script inside clickdetector:

local Remote = game.ReplicatedStorage.ButtonHoverVending

local function mouseEnter(player)
	Remote:FireClient(player, true, script.Parent)
end

local function mouseLeave(player)
	Remote:FireClient(player, false, script.Parent)
end

script.Parent.MouseHoverEnter:Connect(mouseEnter)
script.Parent.MouseHoverLeave:Connect(mouseLeave)

Local Script inside of starterplayerscripts:

local Remote = game.ReplicatedStorage.ButtonHoverVending
local Buttons = {}

Remote.OnClientEvent:Connect(function(Enabled, Button)
	--Create the selection box
	if Enabled then
		local SelectionBox = Instance.new("SelectionBox")
		-- Properties
		SelectionBox.Parent = Button
		--Add the selection box to a table which will store it
		Buttons[Button] = SelectionBox
	elseif Buttons[Button] ~= nil and Enabled == false then
		--The button has a selection box and the server wants to destroy it
		Buttons[Button]:Destroy()
	end
end)

Try setting the Adornee property of the selection box to the button in the local script.

1 Like

You forgot to set the Adornee property.
Edit: @Q_ubit beat me

2 Likes

I have multiple buttons, how would I connect this to all buttons

Edit: oopsie i realised that i just needed to copy and paste script lol

Still doesnt work, code inside localscript:

local Remote = game.ReplicatedStorage.ButtonHoverVending
local Buttons = {}

Remote.OnClientEvent:Connect(function(Enabled, Button)
	--Create the selection box
	if Enabled then
		local SelectionBox = Instance.new("SelectionBox")
		-- Properties
		SelectionBox.Parent = Button
		SelectionBox.Adornee = Button -- the line that i added
		--Add the selection box to a table which will store it
		Buttons[Button] = SelectionBox
	elseif Buttons[Button] ~= nil and Enabled == false then
		--The button has a selection box and the server wants to destroy it
		Buttons[Button]:Destroy()
	end
end)

Do you know if the remote event is being fired or if the selection box is being added the part? Maybe try adding a print at where the remote is fired to see if it gets fired, and printing when the selection box is created.

It still doesnt work. Should i record a video and show you?

I can try testing it out myself in studio

Turns out I made a small mistake, the client script should be this:

Remote.OnClientEvent:Connect(function(Enabled, ClickDetector)
	--Create the selection box
	local Button = ClickDetector.Parent
	if Enabled then
		local SelectionBox = Instance.new("SelectionBox")
		-- Properties
		SelectionBox.Parent = Button
		SelectionBox.Adornee = Button -- the line that i added
		--Add the selection box to a table which will store it
		Buttons[Button] = SelectionBox
	elseif Buttons[Button] ~= nil and Enabled == false then
		--The button has a selection box and the server wants to destroy it
		Buttons[Button]:Destroy()
	end
end)

I was passing the ClickDetector through the parameters and not the button.