SelectionBox not showing on adornee

I’m making a selectionbox script but it isn’t showing up on the adornee

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

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(0.6,0.6,0.6)
selection.Parent = player.PlayerGui

mouse.Move:Connect(function()
	local target = mouse.Target

	if (target) and target.Name == "ClickPart" and target.Parent == script.Parent.Parent then
		selection.Adornee = script.Parent
	else
		selection.Adornee = nil
	end
end)

No errors show up in the output

For SelectionBoxes to work they must be parented to workspace.

Changed the parent to workspace, still doesn’t show up, no errors.

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

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(0.6,0.6,0.6)
selection.Parent = workspace

mouse.Move:Connect(function()
	local target = mouse.Target

	if (target) and target.Name == "ClickPart" and target.Parent == script.Parent.Parent then
		selection.Adornee = script.Parent
	else
		selection.Adornee = nil
	end
end)

Try adding prints through your code and see where it gets up to.

E.g add a print after the if statement: print(string.format("Focusing on %d.", target.Name)).

I’m gonna assume that this LocalScript is inside of a part in the workspace. LocalScripts are not allowed to run in the workspace, only in the locations listed below:


https://developer.roblox.com/en-us/api-reference/class/LocalScript

2 Likes

That’ll probably be the reason why then. Thanks @oskxzr for your suggestions too

Do you have an alternative as in a way I can control the SelectionBoxes of the parts from ReplicatedFirst?

I’d wouldn’t reccomend putting the LocalScript within ReplicatedFirst, since that would result in some other issues and force you to use uncessary :WaitForChild()s.

Put the LocalScript inside StarterGui, then just replace “script.Parent.Parent” inside target.parent == script.Parent.Parent then with the part that it was referencing before. Also, SelectionBoxes don’t need to be parented to the workspace, but it’s a valid option (along with the PlayerGui)

Just adding on to your last point, wouldn’t parenting them to workspace affect it server-side?

09:18:46.013 Infinite yield possible on 'Untitled Game @ 21 Mar 2021 09:18:WaitForChild("Uniforms")' - Studio returns for

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

local selection = Instance.new("SelectionBox")
selection.Color3 = Color3.new(0.6,0.6,0.6)
selection.Parent = workspace

local UniformModels = game:WaitForChild("Uniforms")

mouse.Move:Connect(function()
	local target = mouse.Target

	if (target) and target.Name == "ClickPart" and target.Parent == UniformModels then
		selection.Adornee = script.Parent
	else
		selection.Adornee = nil
	end
end)

What do you mean by affect it server-side?

Also you’re indexing the game with :WaitForChild() instead of the workspace.

local UniformModels = workspace:WaitForChild("Uniforms")

Oh right, I meant to use that, had a brain fart for a second.

Changed game to workspace, still no luck.

Are you sure that Uniforms is parented directly to the workspace?

I’m quite sure yes.
image

Thats odd. Could Uniforms have a trailing space or something?

Also try moving the LocalScript to the StarterGui if you have it in ReplicatedFirst. Your script doesn’t need to be in ReplicatedFirst since it doesn’t need to be ran as soon as possible.

My complete bad, I stillhad selection.Adornee set to script.Parent, which is StarterGui, so now it works, thanks for the help!