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:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function(target)
    if mouse.Target == nil then
        wait()
    elseif mouse.Target.Parent.Name == player.Name then
        print("Cannot select self")
    elseif mouse.Target:IsA("MeshPart") or mouse.Target.Name == "Head" then
        print("Found "..mouse.Target.Parent)
        local box = game:GetService("ReplicatedStorage").Objects.Select:Clone()
            for i,v in next, mouse.Target.Parent:GetChildren() do
                if v:IsA("MeshPart") or v.Name == "Head" then
                    box.Parent = v
            end
        end
    end
end)

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)

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sb = Instance.new("SelectionBox",script.Parent)

mouse.Move:Connect(function()
    local target = mouse.Target
    if target then
        local tPlayer = game.Players:GetPlayerFromCharacter(target.Parent)

        sb.Adornee = tPlayer and tPlayer.Character or nil
        -- You can test true / false when setting values:
        -- if 'tPlayer' exists, then set to 'tPlayer.Character'  or  set to Nil
    end
end)

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.

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

Answer this question