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)
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)