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:
01 | Player = game.Players.LocalPlayer |
02 | Mouse = Player:GetMouse() |
03 |
04 |
05 |
06 | if Mouse.Target:IsA( "BasePart" ) then |
07 | Player.PlayerGui.Selection.Label.Visible = true |
08 | Player.PlayerGui.Selection.Label.X = Mouse.X |
09 | Player.PlayerGui.Selection.Label.Y = Mouse.Y |
10 | Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name |
11 | else |
12 | Player.PlayerGui.Selection.Label.Visible = false |
13 | 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.
Your script could be running before Mouse is set as anything. To prevent this, you'd check if Mouse.Target exists first.
01 | Player = game.Players.LocalPlayer |
02 | Mouse = Player:GetMouse() |
03 |
04 |
05 | if Mouse.Target then |
06 | if Mouse.Target:IsA( "BasePart" ) then |
07 | Player.PlayerGui.Selection.Label.Visible = true |
08 | Player.PlayerGui.Selection.Label.X = Mouse.X |
09 | Player.PlayerGui.Selection.Label.Y = Mouse.Y |
10 | Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name |
11 | else |
12 | Player.PlayerGui.Selection.Label.Visible = false |
13 | end |
14 | end |
Keep in mind that the code will only run once unless it's in a loop, so most likely, nothing will happen.
You can't compare a nil value you must do
01 | Player = game.Players.LocalPlayer |
02 | Mouse = Player:GetMouse() |
03 |
04 |
05 |
06 | if Mouse.Target then |
07 | Player.PlayerGui.Selection.Label.Visible = true |
08 | Player.PlayerGui.Selection.Label.X = Mouse.X |
09 | Player.PlayerGui.Selection.Label.Y = Mouse.Y |
10 | Player.PlayerGui.Selection.Label.Text = Mouse.Target.Name |
11 | else |
12 | Player.PlayerGui.Selection.Label.Visible = false |
13 | end |