How do I fix/make this script?

I’ll explain this in full, basically I have a GUI button where if you click it, it keeps checking if you have a badge (so that if you get it while in-game it also keeps checking), if you don’t have the badge it’ll show one of the Frames saying “You need to try again” and will keep checking to see if you owned the badge, if you already own the badge it will show one of the Frames that has a claim UGC text button (so basically if you get the badge while in-game it will keep rechecking and if you do get it, the GUI where you can claim the UGC appears, the UGC is a free limited), so I wanted to add a script to the textbutton when its clicked that basically checks if your name is in the whitelist, and if it is and you own the badge (it already checked if you own the badge, otherwise this GUI wouldn’t have appeared to you in the first place), it will give you the purchase prompt for the free limited UGC.

How can I make this? I already got everything working except for the last part which is the claim ugc textbutton.

This is the whitelist script: (server script under serverscriptservice)

local Whitelist = {
    --[[Put userIds here to allow them to claim the UGC.
    Or, you could use their roblox name (not case sensitive) instead if you'd like
    ]]
	"Roblox", -- Player Name Example.
	"Builderman",
}

for i, user in Whitelist do
	Whitelist[i] = string.lower(user)
end

game.Players.PlayerAdded:Connect(function(player)
	if table.find(Whitelist,player.UserId) == nil and table.find(Whitelist,string.lower(player.Name)) == nil then
		player:Kick("I do not recognize you...")
	end
end)

This is what the GUI looks like: (under startergui)
RobloxStudioBeta_cS4vGSvzg4

^ Picture Explained: Basically the “BadgeGUI” Frame is the frame that shows if you own the badge, which has a TextButton to claim the UGC called “ClaimButton”, and the “NotiFrame” is the frame that appears with a notification if you don’t own the badge, and the “MainFrame” is the Frame that has a TextButton which checks whether you have the badge or not everytime you click it, and it either shows the “BadgeGUI” if you own the badge, or it shows the “NotiFrame” if you don’t own the badge.

This is the localscript inside the TextButton that is found inside “MainFrame” that checks if you own the badge:

local BadgeService = game:GetService("BadgeService")
local BadgeID = 0 -- Change to your Badge ID
local Gui = script.Parent.Parent.Parent.BadgeGUI -- Change "Frame" to whatever your frame name is which inside the screengui and will open when the player DOES have the badge
local NotiFrame = script.Parent.Parent.Parent.NotiFrame -- Change "NotiFrame" to whatever your frame name is which inside the screengui which will open if the player doesnt have the badge

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, BadgeID)

	if hasBadge then
		Gui.Visible = true
	else
		NotiFrame.Visible = true
		wait(5) -- Here you can change how long the NotiFrame is visable for
		NotiFrame.Visible = false
	end
end)
1 Like

Hi there. Implementing a UGC Limited PromptPurchase must be done from the server (this used to be mandatory, not sure if it’s been updated now), and that’s where RemoteEvents/RemoteFunctions come into play, which is what I’d recommend incorporating for this purpose.

Below is a modification to your server-script that creates a RemoteEvent and parents it to your ReplicatedStorage, and sets up an event-listener to listen for when the client presses the TextButton to claim the item:

-- ServerScript

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local MarketplaceService = game:GetService("MarketplaceService")

local UGCRemote = Instance.new("RemoteEvent")
UGCRemote.Name = "UGCRemote"
UGCRemote.Parent = ReplicatedStorage

local ugcId = 000000

local Whitelist = {
    --[[Put userIds here to allow them to claim the UGC.
    Or, you could use their roblox name (not case sensitive) instead if you'd like
    ]]
	"Roblox", -- Player Name Example.
	"Builderman",
}

for i, user in Whitelist do
	Whitelist[i] = string.lower(user)
end

Players.PlayerAdded:Connect(function(player)
	if table.find(Whitelist,player.UserId) == nil and table.find(Whitelist,string.lower(player.Name)) == nil then
		player:Kick("I do not recognize you...")
	end
end)

UGCRemote.OnServerEvent:Connect(function(plr)
	if table.find(Whitelist, string.lower(plr.Name)) then
		MarketplaceService:PromptPurchase(plr, ugcId)
	end
end)

For your TextButton’s client implementation, since the button will only appear if the player owns the badge in the first place, rechecking for that would render it moderately redundant, hence directly proceed to firing the remote to prompt the UGC’s purchase.

-- LocalScript (Parented to the TextButton)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UGCRemote = ReplicatedStorage:WaitForChild("UGCRemote")

script.Parent.MouseButton1Click:Connect(function()
	UGCRemote:FireServer()
end)

Note that this is just a tentative example implementation to help you get going in the right direction. Modifying this further, implementing cooldowns, other sanity checks, will also be upto you to further improve. Once the player is either “purchased” or “cancelled” the UGC prompt, you can even further use MarketplaceService events and callbacks such as ProcessReceipt or PromptProductPurchaseFinished to check for if the player purchased the item, or cancelled the way, and send back a remote call to the client to update the GUI, etc. to match your preferences.

Hope that helps. Good luck.