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

What is wrong with this script?

Asked by 7 years ago
Edited 7 years ago

I'm trying to make a little GUI pop up and display the name of the Brick the mouse is pointing to. Here is my script:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()



if Mouse.Target:IsA("BasePart") then
    Player.PlayerGui.Selection.Label.Visible = true
    Player.PlayerGui.Selection.Label.X = Mouse.X
    Player.PlayerGui.Selection.Label.Y = Mouse.Y
    Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name
else
    Player.PlayerGui.Selection.Label.Visible = false
end

But I keep getting this error:

Players.Player1.PlayerGui.LocalScript:6: attempt to index field 'Target' (a nil value)

Does anyone know why this is happening.

2 answers

Log in to vote
1
Answered by
shayner32 478 Trusted Moderation Voter
7 years ago

Your script could be running before Mouse is set as anything. To prevent this, you'd check if Mouse.Target exists first.

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()


if Mouse.Target then
    if Mouse.Target:IsA("BasePart") then
        Player.PlayerGui.Selection.Label.Visible = true
        Player.PlayerGui.Selection.Label.X = Mouse.X
        Player.PlayerGui.Selection.Label.Y = Mouse.Y
        Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name
    else
        Player.PlayerGui.Selection.Label.Visible = false
    end
end

Keep in mind that the code will only run once unless it's in a loop, so most likely, nothing will happen.

0
ah you got it just before me! AuthenticOakChair 68 — 7y
0
Oh thank you very much PixelZombieX 8 — 7y
Ad
Log in to vote
1
Answered by 7 years ago

You can't compare a nil value you must do

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()



if Mouse.Target then
    Player.PlayerGui.Selection.Label.Visible = true
    Player.PlayerGui.Selection.Label.X = Mouse.X
    Player.PlayerGui.Selection.Label.Y = Mouse.Y
    Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name
else
    Player.PlayerGui.Selection.Label.Visible = false
end

Answer this question