I made a script where when you have your mouse on a certain part and it shows a GUI that follows your mouse, but it won't work. Can somebody help me with it?
01 | local Plyr = game.Players.LocalPlayer |
02 | local Mouse = Plyr:GetMouse() |
03 | local Object = game.Workspace.Interact 1 |
04 | while true do |
05 | if Mouse.Target = = Object then |
06 | script.Parent.MouseGUI.TextLabel.UDim 2 = UDim 2. new(Mouse.UDim 2 ) |
07 | elseif Mouse.Target ~ = Object then |
08 | wait( 0.0000000000000000000001 ) |
09 | script.Parent.MouseGUI.TextLabel.UDim 2 = UDim 2. new( 0 ,- 500 , 0 , 0 ) |
10 | end |
11 | wait() |
12 | end |
UDim2 is not used directly on the gui. It's used as a Data type to store the Size and Position of the gui. Something like this would work:
01 | local Plyr = game.Players.LocalPlayer |
02 | local Mouse = Plyr:GetMouse() |
03 | local Object = game.Workspace.Interact 1 |
04 | while true do |
05 | if Mouse.Target = = Object then |
06 | script.Parent.MouseGUI.TextLabel.Position = UDim 2. new( 0 ,Mouse.X, 0 ,Mouse.Y) --Mouse.UDim2 doesn't exist. Change to Mouse.X, and Mouse.Y to get where the Offsets of where the mouse is at. |
07 | elseif Mouse.Target ~ = Object then |
08 | wait() --This is the smallest wait time you can do, which defaults to 1/32 of a second. |
09 | script.Parent.MouseGUI.TextLabel.Position = UDim 2. new( 0 ,- 500 , 0 , 0 ) |
10 | end |
11 | wait() |
12 | end |
(Make sure this is in a LocalScript. If it's not, you can't use LocalPlayer)
Anyways, the script should hopefully work how you planned now. If you have any questions or problems, please leave a comment below. Hope I helped :P
You're referencing it's position incorrectly. UDim2
is the format the pixels are sized/positioned in, not the size/position itself. So the correct way to do this is:
1 | script.Parent.MouseGUI.TextLabel.Position = UDim 2. new( 0 ,- 500 , 0 , 0 ) |