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

local UserInputService = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local infogui = game.Players.LocalPlayer.PlayerGui:WaitForChild("InfoUI")


function hover()
 if mouse.Target.Parent.Name == "Item1" then 
  infogui.INFO1.Visible = true
  infogui.INFO1.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
 else
  infogui.INFO1.Visible = false
 end
end

mouse.Move:connect(hover)

Thanks! :)

0
Breaking how? User#5423 17 — 6y

1 answer

Log in to vote
0
Answered by 6 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.

local UserInputService = game:GetService("UserInputService")
local mouse = game.Players.LocalPlayer:GetMouse()
local infogui = game.Players.LocalPlayer.PlayerGui:WaitForChild("InfoUI")

function hover()
    if mouse.Target then -- Check if there is actually a Target
        if mouse.Target.Parent.Name == "Item1" then 
            infogui.INFO1.Visible = true
            infogui.INFO1.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
        else
            infogui.INFO1.Visible = false
        end

    else
        infogui.INFO1.Visible = false -- If there isn't a target, make the infoUI invisible

    end
end

mouse.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 — 6y
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 — 6y
0
The skybox causes mouse.Target to == nil @kingdom5 SHDrivingMeNuts 299 — 6y
Ad

Answer this question