Easy Written By GalaxyGourmet

Premium Membership Benefits Created on: 17-05-2020

Learn how to give benefits to players for having premium.

Checking a Player's Membership

Diving right in to the tutorial, the first thing you want to do before giving players premium benefits is check their membership. To do this, you have to check the player's MembershipType, a property of the player you want to give premium benefits to.

Let's say you had a part in workspace, and that part has a ClickDetector, and a script inside of it. To show an example of giving premium benefits, make use of the MouseClick event of the ClickDetector.

local clickDetector = script.Parent.ClickDetector

clickDetector.MouseClick:Connect(function(player)

end)

Using the player parameter of the MouseClick event, we can check if the player's MembershipType is premium with a simple if statement.

clickDetector.MouseClick:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has premium!")
    end
end)

MembershipType has two Enums: "None", which is when the player does not have any membership, and "Premium", where the player has a premium membership (There are also three other Enums that are builders club related, but builders club is no longer available, and has been replaced with premium).

Prompting Premium

Alright, what if the player doesn't have premium? How can I prompt then to purchase premium? This is very simple to do.

In order to prompt premium, we have to make use of MarketplaceService, and to get the service, you have to put this at the top of the script:

local market = game:GetService("MarketplaceService")

Once you've done that, you're now able to prompt premium purchases to players.

Going back to the MouseClick event, make an else in the if statement we wrote.

clickDetector.MouseClick:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has premium!")
    else
        print(player.Name .. " does not have premium...")
    end
end)

This will make it so anything between the else and the end will run if the player does not have a membership. To prompt premium purchase, we must make use of PromptPremiumPurchase(), which is a function of MarketplaceService, and requires a player as an argument.

In the script, add the PromptPremiumPurchase() function in the else.

clickDetector.MouseClick:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has premium!")
    else
        market:PromptPremiumPurchase(player) --We once again make use of the player parameter from the MouseClick event.
    end
end)

And there you go! Now you prompt premium purchase if the player does not have premium!

PromptPremiumPurchaseFinished

When the player purchases premium from PromptPremiumPurchase(), they do not receive the premium benefits yet. They have to fire the MouseClick event once more to get premium benefits from the script. But we want the player to have premium benefits now. How do we do this? Simple, we have to make use of another event, this time from MarketplaceService. The event is PromptPremiumPurchaseFinished, which fires when a player closes the premium purchase prompt, with or without buying premium.

Below the MouseClick event, insert this in the script:

market.PromptPremiumPurchaseFinished:Connect(function(player)
    --This event also has a player parameter!
end)

Now the player that finished the prompt could've declined the purchase, or they did purchase it, and to find out if they did purchase premium, we can simply check their MembershipType property again!

market.PromptPremiumPurchaseFinished:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has successfully purchased premium! :D")
    end
end)

And there you go! The entire script now detects if the player that clicked the part has premium or not, prompts premium if the player does not have premium, and detects when the prompt closes!

The Final Script

local clickDetector = script.Parent.ClickDetector
local market = game:GetService("MarketplaceService")

clickDetector.MouseClick:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has premium!")
    else
        market:PromptPremiumPurchase(player)
    end
end)

market.PromptPremiumPurchaseFinished:Connect(function(player)
    if player.MembershipType == Enum.MembershipType.Premium then
        print(player.Name .. " has successfully purchased premium! :D")
    end
end)

Easy Written By GalaxyGourmet

See GalaxyGourmet's profile on Roblox

Discussion