(HELP) Script not syncing still?

This post is in reference to one of my last posts here . What we decided to do was to change up our original code a little bit and we are now making it so when you take the Cash, instead of walking 550 studs away you must instead drop off the cash to be awarded it in the leader stats. (I would recommend looking at my last post lol)

The new problem we are having now is that once you walk up to the brick that awards you the cash at the drop off, it is not awarding you the cash nor getting rid of the GUI that says how much cash you have. Here is the updated code:


local function onTouch(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        
        if script.Parent:FindFirstChild(hit.Parent.Name) then
            
        return end
        
        local Player = game.Players:FindFirstChild(hit.Parent.Name)
        
        game.ReplicatedStorage.NameCheck:FireClient(Player)
        
        game.ReplicatedStorage.NameCheck.OnServerEvent:Connect(function(Player, Name)
            playerName = Name
            print(playerName .. Name)
        end)
        
        playerName = hit.Parent.Name
        local part = Instance.new("Part")
        part.Parent = script.Parent
        print(playerName)
        part.Name = hit.Parent.Name
        part.Position = script.Parent.Position
        part.Transparency = 1
        script.Parent.Parent.Bills.Bank.Value = true
    end
    
    showGui()
end

function showGui()
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
            if player.Team == game:GetService("Teams").Police then
            local clone = game.ReplicatedStorage.BankRob:Clone()
            clone.Parent = player.PlayerGui
        end
    end
end

function robberCheck(hit)
    if hit.Parent.Name == playerName then
        endFunction()        
    end
end

function endFunction()
    if game.Workspace.Monet.VaultEnter:FindFirstChild(playerName) then
        local part = game.Workspace.Monet.VaultEnter:FindFirstChild(playerName)
        part:Destroy()
    end
    
    disTanceText2 = 0
    local Player = game.Players:FindFirstChild(playerName)
    Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + Player.robInfo.cashTaken.Value
    Player.robInfo.cashTaken.Value = 0
    Player.PlayerGui.BankGui.Main.Visible = false
    playerName = ""
    
    --script.Parent.Parent.Bills.CoolDown.CoolDownStart:FireClient(Player)
end

script.Parent.Touched:Connect(function(hit)
    if script.Parent.Parent.Bills.Bank.Value == true then
            
    return end
            script.Parent.Parent.Bills.Bank.Value = true
    onTouch(hit)
end)
script.Parent.Parent.DP.Touched:Connect(robberCheck)

I hope one of you out there can decipher this and help us get back on track with our bank lol

Firing to the client just to grab name (in the onTouch method) seems a bit weird. How is this information not accessible on the server? If you are trying to get info from the client, use a remotefunction and do OnClientInvoke (which even I try to avoid unless absolutely needed as a player can manipulate data). Don’t assume the event is going to fire back in a timely manner. That is a wasted connection. I’m also confused with this method as you immediately after do playerName = hit.Parent.Name

EDIT:
I looked more… I see the issue with your code. You are trying to disable the GUI on the server, but the server cannot view the contents of PlayerGui. This must be done with a client sided script, so you’d have to fire an event to close the gui.