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

How to put a selection box around a player when selected?

Asked by
PolyyDev 214 Moderation Voter
7 years ago
Edited 7 years ago

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:

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03 
04mouse.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
18end)

It errors and says 10: attempt to concatenate field 'Parent' (a userdata value)

2 answers

Log in to vote
0
Answered by
hiccup111 231 Moderation Voter
7 years ago
Edited 7 years ago

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)

01local player = game.Players.LocalPlayer
02local mouse = player:GetMouse()
03 
04local sb = Instance.new("SelectionBox",script.Parent)
05 
06mouse.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
15end)
Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You cannot concatenate objects, you have to index the Name property.

1print("Found "..mouse.Target.Parent.Name) --Line 10

Answer this question