Update when TextBox changes

Whenever I type a username, the script says it’s empty, because it’s not being updated upon typing something in.

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local particleEmittersFolder = ReplicatedStorage:WaitForChild("ParticleEmitters")
local premiumParticlesFolder = ReplicatedStorage:WaitForChild("PremiumParticles")

local ModPanelPopup = script.Parent

local function grantParticles(player, particleFolder)
    local character = player.Character
    if character then
        local torso = character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
        if torso then
            for _, particleEmitter in ipairs(particleFolder:GetChildren()) do
                local emitterClone = particleEmitter:Clone()
                emitterClone.Parent = torso

                -- Check for sound
                local sound = particleEmitter:FindFirstChildOfClass("Sound")
                if sound then
                    local soundClone = sound:Clone()
                    soundClone.Parent = torso
                end
            end
        else
            warn("Torso not found for player: " .. player.Name)
        end
    else
        warn("Character not found for player: " .. player.Name)
    end
end

local function grantGodParticles(player)
    grantParticles(player, particleEmittersFolder)
end

local function grantPremiumParticles(player)
    grantParticles(player, premiumParticlesFolder)
end

local function getPlayerFromUsername(username)
    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        if player.Name == username then
            return player
        end
    end
    return nil
end

local function connectUsernameBox()
    local usernameBox = ModPanelPopup.UsernameBox

    -- Define function to set up event handlers
    local function setupEventHandlers()
        usernameBox.FocusLost:Connect(function(enterPressed)
            if enterPressed then
                -- Text entry finished (Enter key pressed)
                local username = usernameBox.Text
                print("Username entered (FocusLost event):", username)
            end
        end)

        -- Update text value in script whenever it changes in the textbox
        usernameBox.Changed:Connect(function()
            local username = usernameBox.Text
            print("Username changed (Changed event):", username) -- For demonstration, you can perform any action here
        end)
    end

    -- Set up event handlers initially
    setupEventHandlers()

    -- Set up event handlers again whenever the value of the username box changes
    usernameBox:GetPropertyChangedSignal("Text"):Connect(function()
        -- Clear existing event handlers
        usernameBox.FocusLost:DisconnectAll()
        usernameBox.Changed:DisconnectAll()
        
        -- Set up event handlers again
        setupEventHandlers()
    end)
end

local function connectGrantButton(button, grantFunction)
    button.MouseButton1Click:Connect(function()
        local username = ModPanelPopup.UsernameBox.Text
        if username ~= "" then
            local player = getPlayerFromUsername(username)
            if player then
                grantFunction(player)
            else
                warn("Player not found with username: " .. username)
            end
        else
            warn("Username is empty!")
        end
    end)
end

-- Call the function to connect event handlers
connectUsernameBox()

-- Connect grant buttons
connectGrantButton(ModPanelPopup.Frame.Grant_GodParticles, grantGodParticles)
connectGrantButton(ModPanelPopup.Frame.Grant_PremiumParticles, grantPremiumParticles)

How would this be solved?

Nevermind, this was a server script. I’ve now made a LocalScript that sends RemoteEvent with values to the ServerScript which then clones the particles and it’s working fine now.