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

Using Mouse.Target.Name works but prints error on output, why?

Hello, devs!

So I was making thing like if you will move your mouse on part then it will change the cursor. It works, but it prints error on the output and It's annoying when you see error on output. (For me.)

CODE:

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

mouse.Move:Connect(function()
	if mouse.Target.Name == "Part" then
		mouse.Icon = "http://www.roblox.com/asset?id=4760454830"
	else
		mouse.Icon = ""
	end
end)

ERRORS:

Workspace.ErktikyYT.LocalScript:5: attempt to index nil with 'Name' Line 5

DETAILS:

LocalScript located in StarterCharacterScripts

1 comment

0

Maybe you should try putting the “mouse” syntax in the function parentheses.

1 answer

✓ Accepted answer

You just need to check if mouse.Target exists

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

mouse.Move:Connect(function()
    if mouse.Target == nil then return end -- check if it exists, if not, return

    if mouse.Target.Name == "Part" then
        mouse.Icon = "http://www.roblox.com/asset?id=4760454830"
    else
        mouse.Icon = ""
    end
end)

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