Stop player selection

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local VictimSelectionPrompt = ReplicatedStorage:WaitForChild("RemoteFunctions"):WaitForChild("VictimSelectionPrompt")

local UIPositions = {
	Visible = UDim2.new(0.403, 0,0.786, 0);
	Hidden = UDim2.new(0.403, 0,1, 0);
}

local TS = game:GetService("TweenService")

local Info = TweenInfo.new(0.5,Enum.EasingStyle.Quart,Enum.EasingDirection.InOut)

local Holder = script.Parent.Holder

local InSelectionMode = false

local VictimHighlight = ReplicatedStorage:WaitForChild("VictimHighlight")

local HighlightColors = {
	Normal = Color3.fromRGB(255, 255, 255);
	Selected = Color3.fromRGB(231, 32, 91);
}

local Executer = game.Players.LocalPlayer

local Mouse = game.Players.LocalPlayer:GetMouse()

local SelectedVictim = nil -- Player who is being considered to execute command on

local ConfirmedVictim = nil -- Player who the command will take effect on (when the executer left clicks on the selected player)

local PreviousHoveredObject = nil

VictimSelectionPrompt.OnClientInvoke = function()
	
	if SelectedVictim ~= nil then SelectedVictim = nil end
	
	if ConfirmedVictim ~= nil then ConfirmedVictim = nil end
	
	InSelectionMode = true
	
	local Tween = TS:Create(Holder,Info,{Position = UIPositions.Visible})
	Tween:Play()
	
	while InSelectionMode do

		for _, plr in pairs(game.Players:GetPlayers()) do

			if plr ~= Executer then

				local PlrChar = plr.Character

				if PlrChar then

					if not PlrChar:FindFirstChildWhichIsA("SelectionBox") then

						local ClonedVictimHighlight = VictimHighlight:Clone()
						ClonedVictimHighlight.Parent = PlrChar
						ClonedVictimHighlight.Adornee = PlrChar

					end

				end

			end

		end
		
		local HoveredObject = Mouse.Target:FindFirstAncestorOfClass("Model")
		
		if HoveredObject then
			
			local HoveredPlr = game.Players:GetPlayerFromCharacter(HoveredObject)
			
			if HoveredPlr then
				
				SelectedVictim = HoveredPlr

				local HoveredPlrChar = HoveredPlr.Character

				if HoveredPlrChar then

					if HoveredPlr.Character:FindFirstChild("VictimHighlight") then

						HoveredPlr.Character.VictimHighlight.Color3 = HighlightColors.Selected

						HoveredPlr.Character.VictimHighlight.SurfaceColor3 = HighlightColors.Selected

					end

				end
				
			end
			
		else

			SelectedVictim = nil

			for _, plr in pairs(game.Players:GetPlayers()) do

				if plr ~= Executer then

					local PlrChar = plr.Character

					if PlrChar then

						if PlrChar:FindFirstChildWhichIsA("SelectionBox") then

							PlrChar:FindFirstChildWhichIsA("SelectionBox").Color3 = HighlightColors.Normal

							PlrChar:FindFirstChildWhichIsA("SelectionBox").SurfaceColor3 = HighlightColors.Normal

						end

					end

				end

			end
			
		end
		
		task.wait()

	end
	
	-- After selection is over, each player's highlight is removed
	
	for _, plr in pairs(game.Players:GetPlayers()) do

		if plr ~= Executer then

			local PlrChar = plr.Character

			if PlrChar then

				if PlrChar:FindFirstChildWhichIsA("SelectionBox") then

					PlrChar:FindFirstChildWhichIsA("SelectionBox"):Destroy()

				end

			end

		end

	end
	
	--print(SelectedVictim)
	
	--print(ConfirmedVictim)
	
	if ConfirmedVictim then
		
		return true,ConfirmedVictim
		
	else
		
		return false,nil
		
	end
	
end

local CancelButton = Holder.CancelButton

CancelButton.MouseButton1Click:Connect(function()
	
	InSelectionMode = false

	local Tween = TS:Create(Holder,Info,{Position = UIPositions.Hidden})
	Tween:Play()
	
end)

--[[
while true do
	
	if InSelectionMode then
		
		for _, plr in pairs(game.Players:GetPlayers()) do
			
			if plr ~= Executer then
				
				local PlrChar = plr.Character
				
				if PlrChar then
					
					if not PlrChar:FindFirstChildWhichIsA("SelectionBox") then
						
						local ClonedVictimHighlight = VictimHighlight:Clone()
						ClonedVictimHighlight.Parent = PlrChar
						ClonedVictimHighlight.Adornee = PlrChar
						
					end
					
				end
				
			end
			
		end
		
	end
	
	task.wait()
	
end
--]]

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
	
	if not gameProcessedEvent then
		
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			
			if SelectedVictim then
				
				ConfirmedVictim = SelectedVictim
				
				InSelectionMode = false
				
				local Tween = TS:Create(Holder,Info,{Position = UIPositions.Hidden})
				Tween:Play()
				
			end
			
		end
		
	end
	
end)

Basically what I want to happen is that when a player hover’s their mouse over another player the selectionbox color changes from white to red and when they stop hovering over that player and hover maybe over idk the sky or the baseplate that player selectionbox is white again or if they stop hovering over the player and hover another the new hovering player gets the red color while the other goes to white again.

I believe the bug starts around the time where the “Hover” variables start. Also, idk if this is related but I got another error called “attempt to index nil FindFirstAncestorOfClass”

1 Like

1. Track the currently hovered player with a variable

local currentHoveredPlayer = nil

2. Update the hover detection logic

Replace this section:

local HoveredObject = Mouse.Target:FindFirstAncestorOfClass("Model")

with:

local hoveredAncestor = Mouse.Target and Mouse.Target:FindFirstAncestorOfClass("Model")

and then check if hoveredAncestor exists.

3. Implement the hover logic

if hoveredAncestor then
    local hoveredPlayer = game.Players:GetPlayerFromCharacter(hoveredAncestor)
    if hoveredPlayer then
        if currentHoveredPlayer ~= hoveredPlayer then
            -- Revert previous hovered player's selection box to white
            if currentHoveredPlayer then
                local prevChar = currentHoveredPlayer.Character
                if prevChar and prevChar:FindFirstChildWhichIsA("SelectionBox") then
                    local selBox = prevChar:FindFirstChildWhichIsA("SelectionBox")
                    selBox.Color3 = HighlightColors.Normal
                    selBox.SurfaceColor3 = HighlightColors.Normal
                end
            end
            -- Highlight new hovered player
            currentHoveredPlayer = hoveredPlayer
            local char = hoveredPlayer.Character
            if char and char:FindFirstChildWhichIsA("SelectionBox") then
                local selBox = char:FindFirstChildWhichIsA("SelectionBox")
                selBox.Color3 = HighlightColors.Selected
                selBox.SurfaceColor3 = HighlightColors.Selected
            end
        end
    end
else
    -- No object hovered, revert previous hovered player's selection box to white
    if currentHoveredPlayer then
        local prevChar = currentHoveredPlayer.Character
        if prevChar and prevChar:FindFirstChildWhichIsA("SelectionBox") then
            local selBox = prevChar:FindFirstChildWhichIsA("SelectionBox")
            selBox.Color3 = HighlightColors.Normal
            selBox.SurfaceColor3 = HighlightColors.Normal
        end
        currentHoveredPlayer = nil
    end
end

Full adjusted snippet inside your loop

Replace the hover detection section with:

local hoveredAncestor = Mouse.Target and Mouse.Target:FindFirstAncestorOfClass("Model")

if hoveredAncestor then
    local hoveredPlayer = game.Players:GetPlayerFromCharacter(hoveredAncestor)
    if hoveredPlayer then
        if currentHoveredPlayer ~= hoveredPlayer then
            if currentHoveredPlayer then
                local prevChar = currentHoveredPlayer.Character
                if prevChar and prevChar:FindFirstChildWhichIsA("SelectionBox") then
                    local selBox = prevChar:FindFirstChildWhichIsA("SelectionBox")
                    selBox.Color3 = HighlightColors.Normal
                    selBox.SurfaceColor3 = HighlightColors.Normal
                end
            end
            currentHoveredPlayer = hoveredPlayer
            local char = hoveredPlayer.Character
            if char and char:FindFirstChildWhichIsA("SelectionBox") then
                local selBox = char:FindFirstChildWhichIsA("SelectionBox")
                selBox.Color3 = HighlightColors.Selected
                selBox.SurfaceColor3 = HighlightColors.Selected
            end
        end
    end
else
    if currentHoveredPlayer then
        local prevChar = currentHoveredPlayer.Character
        if prevChar and prevChar:FindFirstChildWhichIsA("SelectionBox") then
            local selBox = prevChar:FindFirstChildWhichIsA("SelectionBox")
            selBox.Color3 = HighlightColors.Normal
            selBox.SurfaceColor3 = HighlightColors.Normal
        end
        currentHoveredPlayer = nil
    end
end

Regarding the attempt to index nil error

This error occurs if FindFirstAncestorOfClass("Model") returns nil. To prevent this, always check if the result is not nil before using it:

local hoveredAncestor = Mouse.Target and Mouse.Target:FindFirstAncestorOfClass("Model")
if hoveredAncestor then
    -- proceed
end