Need help with switching teams upon buying devproduct

Hello, I’m trying to make a button where if someone clicks it, a dev product pops up for purchase. If the user buys it, it will immediately switch them to either Red team, Yellow team, Green team, or Blue team.

So far I have this:

-- Constants
local RED_TEAM = "Red"
local YELLOW_TEAM = "Yellow"
local GREEN_TEAM = "Green"
local BLUE_TEAM = "Blue"

local player = game.Players.LocalPlayer
-- Developer product ID
local devProductID = 1569120599 -- Replace with the ID of your developer product

-- Function to assign a team to the player
local function assignTeam(player)
	-- Check if the player is already on a team
	

	-- Place the player on a random team
	local randomTeam = math.random(1, 4)

	if randomTeam == 1 then
		player.Team = game.Teams[RED_TEAM]
	elseif randomTeam == 2 then
		player.Team = game.Teams[YELLOW_TEAM]
	elseif randomTeam == 3 then
		player.Team = game.Teams[GREEN_TEAM]
	else
		player.Team = game.Teams[BLUE_TEAM]
	end
end

-- Function to handle button click
local function handleButtonClick(player)
	-- Check if the player has already purchased the developer product
	if player:FindFirstChild(devProductID) then
		assignTeam(player)
	else
		-- Prompt the player to purchase the developer product
		local marketplaceService = game:GetService("MarketplaceService")
		local playerProductId = marketplaceService:GetProductInfo(devProductID).ProductId
		marketplaceService:PromptProductPurchase(player, playerProductId)
	end
end

-- Find the button and connect the click event
local button = script.Parent
button.MouseButton1Click:Connect(handleButtonClick)

To be honest with you, i’m not the best scripter so I often make mistakes or don’t recognize anything. I’ve tried using chatgpt to find a fix but every time it says it finds a fix it never works so I came here!

3 Likes

If you want to check whether they have bought a Developer Product, check on the Server using MarketplaceService.ProccessReceipt, this callback allows you to give Players stuff whether they bought stuff or if the purchase was not completed.

You can look into that here

2 Likes

Can you please show me how to do that though? :slight_smile:

2 Likes