I’m making a chest system that requires an image label to lock to the player’s mouse. However, as soon as my mouse makes any movement, it goes wayyyy off screen to a place where you can’t see it anymore. What can I do to fix this?
UIS.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.fromScale(Position.X,Position.Y)
ItemImage.Position = ImagePosition
wait()
end
end)
Full script if needed:
local UIS = game:GetService("UserInputService")
local Hitbox = script.Parent
local ItemImage = Hitbox.Parent.ItemImage
local ScreenSize = game.Players.LocalPlayer.PlayerGui.ScreenGUI.AbsoluteSize
Hitbox.MouseButton1Click:Connect(function()
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.fromScale(Position.X/ScreenSize.X,Position.Y/ScreenSize.Y)
ItemImage.Position = ImagePosition
UIS.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.fromScale(Position.X,Position.Y)
ItemImage.Position = ImagePosition
wait()
end
end)
end)
Set the offset of the position to the mouse position
UIS.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.new(0, Position.X, 0, Position.Y)
ItemImage.Position = ImagePosition
task.wait()
end
end)
If you want the image directly in the middle of the cursor, set the anchor point of ItemImage to 0.5,0.5
The issue is that the parent (and ascendants/ancestors) of the image kind of offset the image with their size. I’m currently trying to figure out how to get over this
Hitbox.MouseButton1Click:Connect(function()
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
SlotImage.Image = ItemImage.Image
SlotImage.Counter.Text = ItemImage.Counter.Text
SlotImage.Position = ImagePosition
ItemImage.Image = ""
ItemImage.Counter.Text = ""
UIS.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
local Position = UIS:GetMouseLocation()
local ImagePosition = UDim2.fromOffset(Position.X,Position.Y)
SlotImage.Position = ImagePosition
task.wait()
end
end)
end)