TextBox Not updating

For context, I have a code that is trying to find the player’s backpack and tools by using a surface gui. However, when inputting the players username, it doesn’t work even with a focuslost event.

Please help.

local TextButton = script.Parent
local TextLabel = script.Parent.Parent.errorplayernotfound

local toolsaccepted = {"TestTool"}

local function FindPlayerTools(playername)
    local player = game:GetService("Players"):FindFirstChild(playername)
    if player then
        local backpack = player:WaitForChild("Backpack")
        local found = false
        for _, toolname in ipairs(toolsaccepted) do
            local tool = backpack:FindFirstChild(toolname)
            if tool then
                found = true
                print("Found " .. toolname .. " of " .. playername)
                tool:WaitForChild("Cost")
            end
        end
        if not found then
            TextLabel.Visible = true
            wait(3)
            TextLabel.Visible = false
        else
            TextLabel.Visible = false
        end
    else
        TextLabel.Visible = true
        print("Player not found: " .. playername)
    end
    print("Playername: " .. playername)
end

TextBox.FocusLost:Connect(function(enterPressed)
    if not enterPressed then -- Check if the user clicked away from the textbox without pressing Enter
        return
    end
    FindPlayerTools(TextBox.Text)
end)

TextButton.MouseButton1Click:Connect(function()
    FindPlayerTools(TextBox.Text)
end)

Thank you.

I’m assuming this is a server script, since it’s meant to run for every player. Am I correct?

In that case this won’t work because FocusLost only runs on the client.
You should handle this in a local script that has a reference to the SurfaceGui.

If you need the server to handle this, fire a RemoteEvent in your local script.