Attempt to index nil with 'GetFullName'

Mouse=game.Players.LocalPlayer:GetMouse()
Target=Mouse.Target
LocalPlayer=game.Players.LocalPlayer
PlayerGui=LocalPlayer.PlayerGui
Tablet=PlayerGui.ScreenGui.InfoGunTablet

script.Parent.Activated:Connect(function()
	Tablet.PartName.Text=Mouse.Target.Name
	Tablet.FullName.Text=Target:GetFullName()
	Tablet.PartColor.Text=Mouse.Target.BrickColor.Name
end)

This Errors when I use it stating “Attempt to index nil with ‘GetFullName’”
I know I selected the mouse and gui properly because all of that works, its just getting the target ancestors.

Edit: After more messing around it turns out there are multiple properties that turn up with this error when I try to access them. The ones I have found are: Anything having to do with the target’s Ancestry/Descendants, CanCollide, CanTouch, and Anchored. What is the cause of this?

1 Like

it looks as though you forgot to write “Mouse.Target” on the third line

you only wrote “Target”, and so your code assumes you’re talking about a new variable that obviously doesnt exist so its value will be nil

1 Like
Tablet.FullName.Text=Target:GetFullName()

should be

Tablet.FullName.Text = Mouse.Target:GetFullName()
2 Likes

I should’ve specified, mid way through coding I made a Mouse.Target Shortcut.

1 Like

I’m a messy coder, Target is a new variable I made mid way through the stack. I’ll fix the block rq

1 Like

The issue appears to stem from defining Target = Mouse.Target at the top of the script instead of inside the script.Parent.Activated function. Mouse.Target can change dynamically as the mouse moves over different objects or if no object is targeted. By defining Target at the top, it captures the initial value of Mouse.Target and does not update dynamically within the event handler. This can lead to errors if Mouse.Target changes by the time the Activated event is triggered.

To ensure Target reflects the current Mouse.Target value when needed, it should be assigned inside the script.Parent.Activated function. This ensures that you’re accessing the correct object properties at the time of the event, avoiding potential errors related to accessing properties on nil objects or outdated references.

local Mouse = game.Players.LocalPlayer:GetMouse()
local PlayerGui = game.Players.LocalPlayer.PlayerGui
local Tablet = PlayerGui.ScreenGui.InfoGunTablet
local Target  -- Define Target variable

script.Parent.Activated:Connect(function()
    Target = Mouse.Target  -- Assign Mouse.Target to Target inside the function
    if Target then
        Tablet.PartName.Text = Target.Name
        Tablet.FullName.Text = Target:GetFullName()  -- Check if Target is nil-safe
        Tablet.PartColor.Text = Target.BrickColor.Name
    else
        warn("Mouse target is nil")
    end
end)

Or if you’d like without defining target at all at the top,

local Mouse = game.Players.LocalPlayer:GetMouse()
local PlayerGui = game.Players.LocalPlayer.PlayerGui
local Tablet = PlayerGui.ScreenGui.InfoGunTablet

script.Parent.Activated:Connect(function()
    if Mouse.Target then
        Tablet.PartName.Text = Mouse.Target.Name
        Tablet.FullName.Text = Mouse.Target:GetFullName()  -- Check if Mouse.Target is nil-safe
        Tablet.PartColor.Text = Mouse.Target.BrickColor.Name
    else
        warn("Mouse target is nil")
    end
end)

1 Like

Every time you activate the tool and youre pointing at nothing (nil), then youll get that error. So in the Activated event, we add a check to see if the Target is nil or not, if it isnt, then we continue to the rest of the code, which is printing the targets full name. Setting ‘Target’ as a variable essentially ‘snapshots’ the mouse’s target when that line of code is ran, so if you were pointing at a part, itll print out its full name, if it was pointed at nothing, then it would also error. PS: Use local variables. Please.

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local PlayerGui = Player.PlayerGui
local Tablet = PlayerGui.ScreenGui.InfoGunTablet
local Tool = script.Parent 

Tool.Activated:Connect(function()
	if Mouse.Target then -- if there is a target
		Tablet.PartName.Text = Mouse.Target.Name
		Tablet.FullName.Text = Mouse.Target:GetFullName()
		Tablet.PartColor.Text = Mouse.Target.BrickColor.Name
	else -- If no target available
		warn("Target not found.")
	end
end)
1 Like

Cant believe that slipped my mind :man_facepalming: Thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.