Attempt to index nil with 'GetAttributes'

  1. What do you want to achieve?
    Be able to check the attribute/s of all parts that get touched by the mouse.

  2. What is the issue?
    as said in the title, I might sound dumb creating this too yikes.

  3. What solutions have you tried so far?
    I tried searching for other topics but they are irrelevant.

This is the code I am using:

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

while wait(.009) do
	local mouseattributes = mouse.Target:GetAttributes()
	print(mouseattributes)
end
2 Likes

When your mouse isn’t pointing towards any object in the 3D space, mouse.Target returns nil.

You can simply check for this before proceeding further.

-- LocalScript

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()

while task.wait(.009) do
	local target = mouse.Target
	
	if target then
		local mouseattributes = target:GetAttributes()
		print(mouseattributes)
	end
end
1 Like

Yup i was able to "sort of " fix the issue just not the part where it return nil, so thanks.
I also see that you have used GetService is there any difference from using game.players like I did? same thing for task.wait

No worries.

About GetService(), it is generally a better coding practice to “get” services because it returns the Instance of the service by className, and not Name.
You’re able to rename certain services to a custom name and requiring them using GetService ensures your code won’t error regardless of if your service names are manipulated or modified in any form and that you get the correct service no matter what.

This applies to any “paths” that you’ll ever specify using the . operator, such as game.ReplicatedStorage.RemoteFolder.RemoteEvent for example.

1 Like

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