View player backpack on mouse over

Hi everyone,

I want to create a security scanner that shows someone another player’s inventory when their mouse hovers over them. The LocalScript below works, but I am having trouble deleting the text labels once the user removes their mouse from the other player.

LocalScript I wrote:

local p = game.Players.LocalPlayer
local event = script.Parent.RemoteEvent
local pservice = game:GetService("Players")

local frame = script.Parent.Frame
local label = frame.TextLabel

local mouse = p:GetMouse()

local labels = {}
local i = 1

mouse.Move:Connect(function()
	local target = mouse.Target
	if target and target:FindFirstAncestorOfClass("Model") then
		local model = target:FindFirstAncestorOfClass("Model")
		if model:FindFirstChild("Humanoid") then
			local plrToView = pservice:FindFirstChild(model.Name)
			for _, object in pairs(plrToView.Backpack:GetChildren()) do
				if object:IsA("Tool") then
					if not frame:FindFirstChild(object.Name) then
						local newLabel = label:Clone()
						newLabel.Name = object.Name
						newLabel.Parent = frame
						newLabel.Text = object.Name
						table.insert(labels, newLabel)
					else
						return
					end
				else
					return
				end
			end
			label.Text = model.Name.."'s inventory:"
		else
			repeat
				labels[i]:Destroy()
				table.remove(labels, i)
				i += 1
				wait()
			until #labels == 0
			i = 0
			return
		end
	else
		repeat
			labels[i]:Destroy()
			table.remove(labels, i)
			i += 1
			wait()
		until #labels == 0
		i = 0
		return
	end
end)

If anyone could help, that would be great. Thanks!

1 Like

Your problem is here:

for _, object in pairs(plrToView.Backpack:GetChildren()) do
    if object:IsA("Tool") then
        if not frame:FindFirstChild(object.Name) then
            local newLabel = label:Clone()
            newLabel.Name = object.Name
            newLabel.Parent = frame
            newLabel.Text = object.Name
            table.insert(labels, newLabel)
        else
            return
        end
    else
        return
    end
end

You are accessing the backpack of a player and if the items are tools they are getting cloned into the frame. But when you move your mouse away you then destroy the labels and call return.
You want to call return before you clone the labels.
Something like this:

for _, object in pairs(plrToView.Backpack:GetChildren()) do
    if object:IsA("Tool") then
        if not frame:FindFirstChild(object.Name) then
            local newLabel = label:Clone()
            newLabel.Name = object.Name
            newLabel.Parent = frame
            newLabel.Text = object.Name
            table.insert(labels, newLabel)
        end
    end
end

This does not delete the textlabels every time; it only works the first time.

Well Idk what to say because it works for me sounds like its another script.