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

Hovering over a part doesn't work, help?

Asked by 7 years ago

I created a script. It worked the couple of first times, but then it kept breaking. Anyone mind helping?

Here's the script -

01local UserInputService = game:GetService("UserInputService")
02local mouse = game.Players.LocalPlayer:GetMouse()
03local infogui = game.Players.LocalPlayer.PlayerGui:WaitForChild("InfoUI")
04 
05 
06function hover()
07 if mouse.Target.Parent.Name == "Item1" then
08  infogui.INFO1.Visible = true
09  infogui.INFO1.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
10 else
11  infogui.INFO1.Visible = false
12 end
13end
14 
15mouse.Move:connect(hover)

Thanks! :)

0
Breaking how? User#5423 17 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

When I tested your code I recieved the error: Players.TreacherousTale.Backpack.LocalScript:8: attempt to index field 'Target' (a nil value)

To fix this, you must check whether mouse.Target = true.

Furthermore, when the mouse hovers from Item1 to nil (the skybox), the infogui doesn't disappear. To fix this I checked whether mouse.Target = nil, and if it did then you'd set infogui.INFO1.Visible to false.

01local UserInputService = game:GetService("UserInputService")
02local mouse = game.Players.LocalPlayer:GetMouse()
03local infogui = game.Players.LocalPlayer.PlayerGui:WaitForChild("InfoUI")
04 
05function hover()
06    if mouse.Target then -- Check if there is actually a Target
07        if mouse.Target.Parent.Name == "Item1" then
08            infogui.INFO1.Visible = true
09            infogui.INFO1.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
10        else
11            infogui.INFO1.Visible = false
12        end
13 
14    else
15        infogui.INFO1.Visible = false -- If there isn't a target, make the infoUI invisible
16 
17    end
18end
19 
20mouse.Move:connect(hover)

You weren't exactly clear on what the problem was so I tried my best with what I was working with. I hope I helped!

0
mouse.Target will never == true. Only nil and false are falsey in Lua everything else is considered true. User#5423 17 — 7y
0
DIdn't work at all. It said script errors of nothing when I did mine, and this doesn't even do anything. ItzFireable 17 — 7y
0
The skybox causes mouse.Target to == nil @kingdom5 SHDrivingMeNuts 299 — 7y
Ad

Answer this question