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

I have a image that when I click it supposed to be exactly at mouse position but it wont, why?

game:GetService("UserInputService").InputChanged:connect(function(Input)

if Input.UserInputType == Enum.UserInputType.MouseMovement and Dragging == true then
	inputPosition = UDim2.new(0, Mouse.X-25, 0, Mouse.Y-25)
end
	
end)

I even tried to set it too.

game:GetService("UserInputService").InputChanged:connect(function(Input)

if Input.UserInputType == Enum.UserInputType.MouseMovement and Dragging == true then
	inputPosition = UDim2.new(0, Mouse.X, 0, Mouse.Y)
end
	
end)

And changing the pictures anchor points to .5,.5 but no matter what if your windows size is changed the image ends up on the wrong position.

InputPosition is the images position.

heres a gif of what it does https://gfycat.com/RecentDesertedJunco

and when I do change it to fit my screen if I make the window smaller it messes up. How do I fix it so it stays same place no matter window size

5 comments

0

mouse.Button1Down <-- use that mouse.Button1Down:Connect(function()

0

use :Connect() not :connect()

0

that will fix the problem

0

no

0

A D V I C E, also you could use UserInputService for detecting mouse input

1 answer

Try this in a LocalScript

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

while true do
	local xm = mouse.X
	local ym = mouse.Y
	script.Parent.Position = UDim2.new(0, xm, 0, ym)
	wait()
end

Unfortunately, the Mouse Position can only be read using the offset, rather than the scalar Vector3 properties.

However, you could try this, which would take into account the mouse's position in relation to the absolute screen size, but is an estimation of the placement.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

while true do
	local xm = mouse.X
	local ym = mouse.Y
	local xg = script.Parent.AbsoluteSize.X
	local yg = script.Parent.AbsoluteSize.Y
	script.Parent.Position = UDim2.new((1-(xg/xm)), 0, (1-(yg/(2*ym))), 0)
	wait()
end

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