I have a game and I want a selection box to appear when the mouse hovers over the player. This is the script I have so far:
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 |
04 | mouse.Move:Connect( function (target) |
05 | if mouse.Target = = nil then |
06 | wait() |
07 | elseif mouse.Target.Parent.Name = = player.Name then |
08 | print ( "Cannot select self" ) |
09 | elseif mouse.Target:IsA( "MeshPart" ) or mouse.Target.Name = = "Head" then |
10 | print ( "Found " ..mouse.Target.Parent) |
11 | local box = game:GetService( "ReplicatedStorage" ).Objects.Select:Clone() |
12 | for i,v in next , mouse.Target.Parent:GetChildren() do |
13 | if v:IsA( "MeshPart" ) or v.Name = = "Head" then |
14 | box.Parent = v |
15 | end |
16 | end |
17 | end |
18 | end ) |
It errors and says 10: attempt to concatenate field 'Parent' (a userdata value)
Set the SelectionBox's Adornee to the player's Character Model. Also, you can shorten the detection of the player by using the:
Try this: (LocalScript in StarterCharacter)
01 | local player = game.Players.LocalPlayer |
02 | local mouse = player:GetMouse() |
03 |
04 | local sb = Instance.new( "SelectionBox" ,script.Parent) |
05 |
06 | mouse.Move:Connect( function () |
07 | local target = mouse.Target |
08 | if target then |
09 | local tPlayer = game.Players:GetPlayerFromCharacter(target.Parent) |
10 |
11 | sb.Adornee = tPlayer and tPlayer.Character or nil |
12 | -- You can test true / false when setting values: |
13 | -- if 'tPlayer' exists, then set to 'tPlayer.Character' or set to Nil |
14 | end |
15 | end ) |
You cannot concatenate objects, you have to index the Name
property.
1 | print ( "Found " ..mouse.Target.Parent.Name) --Line 10 |