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
5 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 — 5y

1 answer

Log in to vote
2
Answered by 5 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 — 5y
0
NP XxTrueDemonxX 362 — 5y
Ad

Answer this question