Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

It works but not always....?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)
0
Is that the full script? Mauvn 100 — 9y
0
yes iipartyhat 25 — 9y

1 answer

Log in to vote
1
Answered by
Merely 2122 Moderation Voter Community Moderator
9 years ago

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
Ad

Answer this question