Still have questions? Join our Discord server and get real time help.
Log in to vote

It works but not always....?

Asked by 4 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()
if mouse.Target.Name=="derpbrick" then,mouse.X,0,mouse.Y)

    end end)
Is that the full script? Mauvn 100 — 4y
yes iipartyhat 25 — 4y

1 answer

Log in to vote
Answered by
Merely 2085 Moderation Voter Community Moderator
4 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,mouse.X,0,mouse.Y)

Answer this question