Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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.

1 answer

✓ Accepted answer

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

2 comments

0

tysm :)

0

NP

This discussion is preserved as it was. New answers, comments, and votes are closed.