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

How to change position of text box with mouse position?

Asked by 5 years ago
Edited 5 years ago

I want to make a textbox appear next to my mouse when I right click. This is the code I have but it is not working. It is a local script in starter player scripts.

wait(1)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local popup = player.PlayerGui.screen.test
local x = mouse.Hit.X
local y = mouse.Hit.Y

mouse.Button1Down:connect(function()
    popup.Visible = false
    player.Character.Humanoid:MoveTo(mouse.Hit.p)
end)

mouse.Button2Down:connect(function()
    popup.Visible = false
    popup.Position = UDim2.new(x, y)
    popup.Visible = true
end)
0
try UDim2.new(x,0,y,0) or UDim2.new(0,x,0,y) greatneil80 2647 — 5y
0
I tried UDim2.new(0,x,0,y) the first time, and it seems to work in that it fully cycles through the function, but it does not update the position. Using UDim2.new(x, y) or UDim2.new(x,0,y,0) gets to the first line and stops when it reaches the second, leaving it not visible and not changing the position. GlideSquared 2 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

The problem here is that Mouse.Hit is a CFrame value while the position of a gui is a UDim2 value, and those two do not match well. For this, you will have to use a UDim2 value. The basic UDim2 value can be summed up as such:

Udim2.new(x_scale,x_offset,y_scale,y_offset)

We can access the x offset and the y offset of the mouse with the X and Y properties of the mouse.

local mouse = game.Players.LocalPlayer:GetMouse()
print(mouse.X,mouse.Y)

For example, I can have a gui follow my mouse with either the mouse.Move event or the renderstepped event of the Run Service.

local gui =  script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local rs = game:GetService("RunService")
rs.RenderStepped:Connect(function()
    gui.Position = UDim2.new(0,mouse.X,0,mouse.Y)
end)

However, in your case of the Mouse.Button2Down event, the same principles can be applied

mouse.Button2Down:Connect(function()
    popup.Visible = false
    popup.Position = UDim2.new(0,mouse.X,0, mouse.Y)
    popup.Visible = true
end)
0
Thank you for the answer! This works perfectly :) GlideSquared 2 — 5y
Ad

Answer this question