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

Why is Target in mouse.Target nil all a sudden?

Asked by
chafava -113
4 years ago
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

target = mouse.Target
local billboard

while true do
    if target.Parent == game.Workspace.Items then
           billboard = mouse.Target.BillboardGui
        billboard.Enabled = true

        print("Mouse hovering over food")
    else
         billboard.Enabled = false

    end

    wait(.1)
end

target is fine, but target.Parent isn't why not?

local script in starter pack

0
mouse.Target can be nil if the mouse doesn't have a target. Code block if mouse.Target is not nil before you do anything. pidgey 548 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

Your target is coming up nil because you're only checking for the mouse target as the script is loading so it is never actually updating what the mouse is targeting. Try something like this:

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

while true do
    local target = mouse.Target --Mouse target will update at the start of every loop

    if target ~= nil and target.Parent == game.Workspace.Items then --Makes sure the target isn't nil such as aiming the mouse at the sky
        billboard = mouse.Target.BillboardGui
        target.BillboardGui.Enabled = true

        print("Mouse hovering over food")
    else if billboard ~= nil then --Makes sure the script doesn't error should the billboard not exist
        billboard.Enabled = false
        end
    end

    wait(.1)
end
0
tysm :) chafava -113 — 4y
0
NP XxTrueDemonxX 362 — 4y
Ad

Answer this question