How to target Nearest Part using GUI

Hey guys, I was wondering how I would be able to script a GUI button to damage the selected part. Example: You click a part, and the part you just selected will become the target. Using a button that appears at the bottom of the screen, you do damage to the part. depending on the tool you have the more damage it does.

Can someone please at least try and help me understand a simple and efficient way to do this? Thanks!

-DeeCoded

1 Like

You can try messing with Click Detector

1 Like

So are you,

a. Trying to make a button that damages the closest part?
b. Trying to make a button that damages a selected part?

To get the part the mouse is targeting, use:

game:GetService("Players").LocalPlayer:GetMouse().Hit

If the user is not targeting a part, that will return nil

Then tie it to UserInputService like:

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessed)
  if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
    local target = game:GetService("Players").LocalPlayer:GetMouse().Hit
    if target then
      -- do something, namely create a gui and store the target so that whenever a button is pressed it does something
    end
  end
end)

that seems like it will work thank you for the reply!

i am trying to make a button that damages the selected part, i should have worded it better i am sorry.

1 Like