Search code examples
luaroblox

why isnt the gui opening when i touch the part?


I have tried almost everything i can to fix it but im not gettig no error message so i dont know what to do i was wondering if one of you can help me solve this issue here is the code:

local frame = game.StarterGui.ShopGui.AURA_SHOP_NAME
local frame2 = game.StarterGui.ShopGui.MAIN_FRAME
local part = game.Workspace["Aura shop"].Shop.ShopPart

part.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        frame.Visible = true
        frame2.Visible = true
    end
end)

part.TouchEnded:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        frame.Visible = false
        frame2.Visible = false
    end
end)

Solution

  • Assuming this is a Script, you have three issues.

    1. Scripts execute on the server, so they don't have access to a player's UI elements, which are only visible on the client. (With some exceptions). So you'll need to convert this to a LocalScript or use a RemoteEvent to communicate between the server and client.
    2. You are modifying the UI in the StarterGui, which are not the same elements that are visible to a player. Those elements are copied into the PlayerGui when their character spawns.
    3. Many parts on your character model can fire the Touched and TouchEnded signals, so you need to do some simple accounting to only hide the UI once all of the parts stop touching.

    To fix these issues, add a RemoteEvent to ReplicatedStorage, and try this :

    -- find the remote event
    local remoteEvent = game.ReplicatedStorage.RemoteEvent
    
    -- keep a dictionary to count how many parts are touching the shop
    local counts = {} -- [player name] : counter 
    
    -- find the shop part
    local part = game.Workspace["Aura shop"].Shop.ShopPart
    
    part.Touched:Connect(function(hit)
         -- when a player touches the shop, add one to the counter
         local player = game.Players:GetPlayerFromCharacter(hit.Parent)
         if player then
             if not counts[player.Name] then
                 counts[player.Name] = 0
    
                 -- a player has just started touching the shop, show the screen
                 remoteEvent:FireClient(player, true)
             end
    
             counts[player.Name] += 1
        end
    end)
    
    
    part.TouchEnded:Connect(function(hit)
        -- when the player stops touching, subtract one from the counter
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then
            counts[player.Name] -= 1
    
            if counts[player.Name] == 0 then
                -- all the parts have stopped touching the shop, hide it
                remoteEvent:FireClient(player, false)
                counts[player.Name] = nil
            end
        end
    end)
    

    Then put a LocalScript in the StarterGui, it will listen for when the server tells the client to show the shop.

    -- find the UI elements
    local shopGui = script.Parent.ShopGui
    local frame = shopGui.AURA_SHOP_NAME
    local frame2 = shopGui.MAIN_FRAME
    
    -- find the remote event
    local remoteEvent = game.ReplicatedStorage.RemoteEvent
    
    -- listen for messages from the server
    remoteEvent.OnClientEvent:Connect(function(isVisible)
        frame1.Visible = isVisible
        frame2.Visible = isVisible
    end)