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)
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)
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.
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)
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.
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.