Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I need a SelectionBox to appear under multiple conditional statements?

Asked by 6 years ago

This script is meant to have a selectionbox appear over a block if your mouse is aimed at that block and that block is a certain height. This is the script:

while wait() do  
  if script.Parent.Size.Y >= 8 then
        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
        local selection = Instance.new("SelectionBox")
        selection.Color3 = Color3.new(0.6,0.6,0,6)
        selection.Parent = player.PlayerGui
        mouse.Move:connect(function() 
          local target = mouse.Target
          if not target then
            selection.Adornee = nil
          elseif target == script.Parent then
            selection.Adornee = target
      else 
        selection.Adornee = nil
end                
    end)
        end

This works without the size constraint, how do I fix it to work under the size constraint. Do I use and?

0
Is this script inside a part in the workspace, or inside the startergui? UgOsMiLy 1074 — 6y
0
Inside a part in the workspace. TiredMelon 405 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The code is a lot simpler than it looks, I have used a LocalScript inside StarterPlayer > StarterCharacterScripts to actually make it work & with the size constraint as you mentioned.

local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();

-- // Selection Creation

local selection = Instance.new("SelectionBox");
selection.Color3 = Color3.new(0.6, 0.6, 0.6)
selection.Parent = script.Parent;

-- // Event

mouse.Move:Connect(function()
    local target = mouse.Target;

    if not target then
        selection.Adornee = nil;


    elseif target and target.Size.Y >= 8 then -- Filters if target exists & its height is higher than 8.
        selection.Adornee = target;
    end
end)

It is pretty self-explanatory, every part that the mouse targets and has anY coordinatebigger than the one defined in the code will be selected with an adornee. Take care that if there is no target or if it doesn't exist it won't actually add an Adornee. If you want to filter the parts that can be selected you can also add a table and filter by name.

Hope this helped!

Modified ~

As you want it to only work for a specific part, you can actually identify the part by the actual name, in this case we have the part named** "Pizza"** that will only get selected if it has an Y coordinate higher than 8. Altough if you want to add more parts it would be more efficient if you actually added a table with the parts that you especifically want to get adorneed.

local plr = game.Players.LocalPlayer;
local mouse = plr:GetMouse();

-- // Selection Creation

local selection = Instance.new("SelectionBox");
selection.Color3 = Color3.new(0.6, 0.6, 0.6)
selection.Parent = script.Parent;

-- // Event

mouse.Move:Connect(function()
    local target = mouse.Target;

    if target and target.Size.Y >= 8 and target.Name == "Pizza" then -- Filters if target exists & its height is higher than 8 and with name "Pizza".
        selection.Adornee = target;
    else
        selection.Adornee = nil;
    end
end)

Hope this helped you, once again!

0
This is for any part bigger than that, however I only want it for this certain part, not for any part greater than that size. Maybe since it isn't in a LocalScript. TiredMelon 405 — 6y
0
This is a simple solution, thank you. TiredMelon 405 — 6y
Ad

Answer this question