Hello!
I have recently been trying to position a frame wherever the player clicks Here's what I did:
StarterGui/ScreenGui
StarterGui/ScreenGui/Button
StarterGui/ScreenGui/Button/LocalScript
StarterGui/ScreenGui/Button/Frame
Then I tried two methods... First one was relatively simple:
local button = script.Parent
local frame = button:WaitForChild("Frame")
local userInputService = game:GetService("UserInputService")
button.MouseButton1Click:Connect(function()
local pos = userInputService:GetMouseLocation()
frame.Position = UDim2.new(0, pos.X, 0, pos.Y)
end)
The achieved result was this
That didn't work for some reason... So I tried a little different approach:
local button = script.Parent
local frame = button:WaitForChild("Frame")
local userInputService = game:GetService("UserInputService")
button.MouseButton1Click:Connect(function()
local pos = userInputService:GetMouseLocation()
local relativePosition = pos - frame.Parent.AbsolutePosition
frame.Position = UDim2.fromOffset(relativePosition.X, relativePosition.Y)
end)
I got the exact same result. Both cases, the frame was position well above the click.
I don't understand why.
Any help would be appreciated.
Kind regards,
Rod455
