Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find out where a player clicks on a button?

So i'm trying to make an effect similar to where when you click on a button (ex: shop) a ring appears then the ring going outwards based on where you click and the ring is under a frame that is clipsdesendents true.

I am lost on where to start with this. All help is appreciated!!

1 answer

✓ Accepted answer

This is possible by using mouse.X and mouse.Y combined with mouse.ViewSizeY and mouse.ViewSizeX to determine the location on the screen. Though these can only be defined seperatly so it’s easiest to write a function for retrieving them when desired, instead of a mess of variables.

--// Position / Size is the formula for normalizing the 2D location, this is written between 0~1 for both axis’. (There isn’t much to worry about this)
function GetMouse2D(Player)
   if (game.Players[Player]) then
      local Mouse = Player:GetMouse()
      return Vector2.new(Mouse.X, Mouse.Y) / Vector2.new(Mouse.ViewSizeY, Mouse.ViewSizeX)
   end
end

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
   if (InoutObject.UserInputType == Enum.UserInputType.MouseButton1) then
      local MousePos = GetMouse2D()
      print(MousePos)
   end
end)

Hope this helps! If so, don’t forget to accept and upvote!

8 comments

0

That would work except for the fact that it would expand over the button and look weird. Im asking how to find the position of the mouse click relative to the button that the person clicked.

0

You’d fire this function inside the Click Listener. This would return the position presumably relative to the Button

0

With the code listed I dont think so? If I get just the position of the mouse and then parent it into a frame inside the button and position it to mouse.X and mouse.Y then it will be far off the screen.

0

Let me explain this a bit better. I want to position a ring to a frame which has the size of the button and it is parented to the button. I want the ring to be where you clicked on the button.

0

You’d use UserInputService or ContextActionService for this, then you can use ViewSize to get the normalized position

0

That should work?

0

How would I code that?

0

I’ve redone my answer as I said

This discussion is preserved as it was. New answers, comments, and votes are closed.