How can I create a script that only makes a GUI accessible to those who own a gamepass?
An example would be a Dance GUI with several dances, that the player only has access to after buying the “Dance Bundle” gamepass.
How can I create a script that only makes a GUI accessible to those who own a gamepass?
An example would be a Dance GUI with several dances, that the player only has access to after buying the “Dance Bundle” gamepass.
Start using DataStore, create a table for GamePass players, and then you will be able to compare the current player with the registered player.
You could use MarketplaceService | Roblox Creator Documentation or MarketplaceService | Roblox Creator Documentation.
-- Example code
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(PLAYER_USERID, GAMEPASS_ID) then
--[[ Context
"PLAYER_USERID" is the player's UserId
"GAMEPASS_ID" is the id for the game pass
The function returns either true or false depending on whether or not the player owns the gamepass.
This function can also fail so it should be set inside a pcall.
--]]
-- Enable the gui
end
Move the GUI into ServerStorage if you have one. Then, create a script inside ServerScriptService:
--Code
local UI = game.ServerStorage.YourUI:Clone() --clone ui
local gamepassID = --your gamepass id
local MS = game:GetService("MarketPlaceService")
--now check if the player has the gamepass
game.Players.PlayerAdded:Connect(function(player) --player added event
if MS:UserOwnsGamePassAsync(player.UserId, gamepassID) then --check if player has gamepass
UI.Parent = player.PlayerGui --set clone parent to playergui
UI.Enabled = true --set gui enabled to true (optional)
end
end)
This is a waste of a datastore since a function like MarketplaceService:UserOwnsGamePassAsync() exists. All though you do use datastores for products.
I didn’t know this. One more thing learned. Thank you.
No problem dude! Happy to help a person out.
This is the script once I finished everything. The GUI doesn’t show up on the screen once I click play:
local UI = game.ServerStorage.DancePack1:Clone() --clone ui
local gamepassID = 20138005
local MS = game:GetService("MarketplaceService")
--now check if the player has the gamepass
game.Players.PlayerAdded:Connect(function(player) --player added event
if MS:UserOwnsGamePassAsync(player.UserId, 20138005) then --check if player has gamepass
UI.Parent = player.ServerStorage --set clone parent to playergui
UI.Enabled = true --set gui enabled to true (optional)
end
end)
Well, this is weird…I will try to figure it out.
Many thanks! Hope it figures itself out fairly easily.
Good luck!
I figured it out, because of the line 8 in the script. Where it said the UI parent to the ServerStorage, which is not located in the Players, I decided to change it into PlayerGui and it is work perfectly. I’m very happy to helped. Here is the fixed script:
local UI = game.ServerStorage.DancePack1:Clone() --clone ui
local gamepassID = 20138005
local MS = game:GetService("MarketplaceService")
--now check if the player has the gamepass
game.Players.PlayerAdded:Connect(function(player) --player added event
if MS:UserOwnsGamePassAsync(player.UserId, 20138005) then --check if player has gamepass
UI.Parent = player.PlayerGui --set clone parent to playergui
UI.Enabled = true --set gui enabled to true (optional)
end
end)