Need help with a script that gives tools from Server storage to a team

  1. What do you want to achieve?
    I want to make a script that would give tools to a team

  2. What is the issue?
    image
    I don’t know how to make a team get tool(s)

  3. What solutions have you tried so far?
    Did you look for solutions on the Developer Hub?
    ^^^^^ Yes, “MarketplaceService”

The script would give a team tools (the tools are in ServerStorage) after a
Dev Product is purchased by a player.
(Script would be executed when a player clicks on a click detector. It would have a 30 minute cooldown. You can buy the product after clicking on a gui)

This is the script (it is a child of the value, if the value is true the team gets the tools, after respawning too)

game:GetService("MarketplaceService"):PromptPurchase(1102270834)
if game:GetService("MarketplaceService"):PromptPurchaseFinished(1102270834) then
	script.Parent = true
	
	if script.Parent = true then
		game.Teams["Class-D"].
		wait(600)
		script.Parent = false
	end

also this is what would happen after the purchase:
image

1 Like

PromptPurchaseFinished is an event which you connect upon, additionally PromptProductPurchaseFinished is specifically for DevProduct purchases - same as PromptProductPurchase that I advise you change to.

PromptProductPurchaseFinished however is deprecated and superseded by a more secure ProcessReceipt callback. In this callback you can specify if the processing completed successfully through returning a ProductPurchaseDecision, otherwise it’ll try again once they rejoin. It receives an information dictionary as a parameter, containing the PlayerId and etc.

So, we’ll want to assign a function to this callback and handle if the player purchased that product. If this is the case, we’ll add those tools into the Team’s players’ backpacks (and StarterGear too if you want them to retain them after dying). Let’s show an example:

--// This must be done in a Script to access ServerStorage & assign ProcessReceipt

--// Services
local server_storage = game:GetService("ServerStorage");
local marketplace_service = game:GetService("MarketplaceService");
local players = game:GetService("Players");

--// Constants
local PRODUCT_ID = 1 --// Be sure to set this correctly

--// Functions
local function process_receipt(info)
    local player = players:GetPlayerByUserId(info.PlayerId); --// Must be used

    if (player) then --// The player could've left
        if (info.ProductId == PRODUCT_ID) then --// Is it our product?
            local team_players = player.Team:GetPlayers(); --// Get the players on our Player's team

            for _, team_player in ipairs(team_players) do
                 server_storage.Tool:Clone().Parent = team_player.Backpack --// Clone the Tool from ServerStorage and parent it to their Backpack
            end
        
            return Enum.ProductPurchaseDecision.PurchaseGranted --// We successfully granted them the tools
        end
    end

    --// If it returns above, this'll never run
    return Enum.ProductPurchaseDecision.NotProcessedYet; --// We haven't given them the tools, it'll retry when they rejoin
end

--// Callbacks
marketplace_service.ProcessReceipt = process_receipt;
2 Likes

Try this:

for i,v in pairs(game.Players:GetPlayers()) do
    if v.Team == "Class-D" then
            print("Giving")
            -- code
    end
end

Make sure to test while using player purchases - you don’t wanna scam people!

1 Like

you mean buying my own stuff???

In studio it should say that it is a test purchase and that it will not charge any robux.

1 Like

You first need to understand what you’re doing before you get into specific implementation. I encourage you to read up on working with Developer Products first, as that contains information about them as well as relevant code samples you’ll need to get this job done. Always take a look at the Developer Hub if you get stuck some place, just to confirm that you’re using API correctly.

The next step after that is to use what you’ve researched to try and make a script. From there, you can also try adding any additional items that you want including your specific use case, which I’m assuming here is meant to be a product that grants tools to a team on purchase for an allotted period of time before removing them.

You might also want to familiarise yourself with other things too, such as what the Parent property is - a script can’t be parented to a non-Instance.

For team giving script you can do.

--// Settings
TeamColorValue = -- insert here

--// Variables
local Players = game:GetService("Players")
local ToolsFolder = game.ServerStorage:WaitForChild("Tools")
local Tool = ToolsFolder:WaitForChild("Tool")

--// Main Code
Players.PlayerAdded:Connect(function(Player)
   for _,plr in pairs(game.Players:GetChildren()) do
       if plr.Team == Color3.fromRGB(TeamColorValue) then
          local tool_C = Tool:Clone()
          tool_C.Parent = plr:FindFirstChild("Backpack")
       end
    end
end)
1 Like