GUI not poping in-game, but it works in studio

The code in a part:
local db = false
game.Players.PlayerAdded:Connect(function(plr)
script.Parent.Touched:Connect(function(hit)
if not db then
db = true
local ShopUI = game.ServerStorage.UIs.ShopUI
local ShopUIClone = ShopUI:Clone()
ShopUIClone.Parent = plr.PlayerGui
script.Parent.TouchEnded:Connect(function()
ShopUIClone:Destroy()
db = false
end)
end
end)
end)
Basically, the ScreenGui is in a folder named UIs which is placed in ServerStorage, when I tested, it worked fine in studio. But when I switch to game, it just doesn’t work.

Why a PlayerAdded even though? You don’t really need that, unless if you want to add specific objects to players

You only just need to reference the Touched event’s parameter, which is the “HitPart” that it returns back

local db = false

script.Parent.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

    if not db and Player then
        db = true
        local ShopUI = game.ServerStorage.UIs.ShopUI
        local ShopUIClone = ShopUI:Clone()
        ShopUIClone.Parent = Player.PlayerGui

        script.Parent.TouchEnded:Connect(function()
            ShopUIClone:Destroy()
            db = false
        end)
    end
end)

Also I would seriously not recommend using a TouchEnded event when the part you’re attempting to touch has ended, please use something else like Region3 or Magnitude instead

1 Like