Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.