I'm trying to make a SelectionBox and a GUI to appear once I put my Mouse on that part. I got that part over with, but I'm stumped when it comes to removing the SelectionBox and the GUI once the mouse is not detecting that part anymore. I've already tried a method but since there will be a lot of parts around the map it doesn't work correctly.
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local Previous = "" Mouse.Move:connect(function() if Mouse.Target.Name == "Plant" then local hitbox = Instance.new("SelectionBox", Mouse.Target) hitbox.LineThickness = 0.05 hitbox.Adornee = Mouse.Target Player.PlayerGui.ScreenGui.Frame.Visible = true Player.PlayerGui.ScreenGui.Frame.TextLabel.Text = Mouse.Target.Hits.Value .. "/ 3" Previous = Mouse.Target.Name else workspace[Previous].SelectionBox:remove() Player.PlayerGui.ScreenGui.Frame.Visible = false end end)
It's located in a LocalScript in StarterGui
there is more than one part, so if it looks for it, it might look for the wrong one. by setting it as an object, you get directly to the object instead of looking for in in the workspace.
local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local Previous = nil local selected = false Mouse.Move:connect(function() if Mouse.Target.Name == "Plant" then if selected == false then selected = true local hitbox = Instance.new("SelectionBox", Mouse.Target) hitbox.LineThickness = 0.05 hitbox.Adornee = Mouse.Target Player.PlayerGui.ScreenGui.Frame.Visible = true Player.PlayerGui.ScreenGui.Frame.TextLabel.Text = Mouse.Target.Hits.Value .. "/ 3" Previous = Mouse.Target end else if Previous then if Previous:FindFirstChild("SelectionBox") then Previous.SelectionBox:remove() Player.PlayerGui.ScreenGui.Frame.Visible = false end end selected = false end end)