Gamepass gui giver

I am trying to make it so if you own a gamepass it gives you a gui you can open and close. but the problem is that When it detects the player owns the gamepass it gives it to him but if the player resets the gui disappears. Here is my code:

local UI = game.ServerStorage.Guis.RadarGui:Clone() --clone ui
local gamepassID = 17814605
local ms =  game:GetService("MarketplaceService")


game.Players.PlayerAdded:Connect(function(player) --player added event
	if ms:UserOwnsGamePassAsync(player.UserId, gamepassID) then
		print("Owns radar")
		UI.Parent = player.PlayerGui 
		UI.Enabled = true 
	end
end)

this is a Script in ServerScriptService. Another problem is that I want it to appear and disappear when the player that owns it clicks L.

1 Like

I think it should be placed in starter player scripts since it’s being ran on a player not the server.

2 Likes

If i switch places it wont even appear the gui

Toggle off ResetOnSpawn in the ScreenGui properties

Turn off the ResetOnSpawn property for ‘RadarGui’, assuming it’s a ScreenGui

Also, you’re cloning the UI only once. If anyone else happens to join with the gamepass, the script will error. You should clone it for every time that a Player joins the game, as such:

local gamepassID = 17814605
local ms =  game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(player) --player added event
	if ms:UserOwnsGamePassAsync(player.UserId, gamepassID) then
        local UI = game.ServerStorage.Guis.RadarGui:Clone() --clone ui
		print("Owns radar")
		UI.Parent = player.PlayerGui 
		UI.Enabled = true 
	end
end)
2 Likes

It works! Quick question. How would i make it so if press L it opens and closes

1 Like

You should post another topic for this as it is currently straying off-topic. You’ll have to put a LocalScript inside of the GUI and make use of UserInputService.

1 Like