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

It works but not always....?

ok so this works but when i move my mouse off of the game frame it prints that target is nil

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Move:connect(function()
if mouse.Target.Name=="derpbrick" then 
game.Players.LocalPlayer.PlayerGui.herpy.Position=UDim2.new(0,mouse.X,0,mouse.Y)
	
	end end)

2 comments

0

Is that the full script?

0

yes

1 answer

✓ Accepted answer

Open up the Output window and you'll notice an error occurred: 22:36:05.540 - Players.Player1.PlayerGui.LocalScript:3: attempt to index field 'Target' (a nil value)

The problem here is that you're assuming that mouse.Target is an object. When you move your mouse over the sky, there's nothing that the mouse is pointing to, so mouse.Target becomes nil. When you try to get the value of the "Name" property of nil, of course it's going to throw an error!

Before checking the name property, you should make sure that mouse.Target exists.

if (mouse.Target ~= nil and mouse.Target.Name == "derpbrick" then
	   game.Players.LocalPlayer.PlayerGui.herpy.Position=UDim2.new(0,mouse.X,0,mouse.Y)
end

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