Issues with creating in-game gamepass script

I’ve been working on a few scripts in order to handle gamepasses for my game. This is the last script I’ve got to do.

It’s a handler script that basically checks if a player purchased a gamepass in-game (e.g. from a clickable button, or a shop gui, etc.). From there on, it’ll give the player effects. I want to be able to give tools to the player, once they’ve purchased the gamepass in-game.

Thus far I have tried to make this possible myself, but to no avail. I have tested if the script works by adding a fire effect, and after testing it out, it does actually work.

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

local gamePassId = 25817134

local function giveGamePassPerks(player)
	if player.Character then
		local flame = Instance.new("Fire")
		flame.Parent = player.Character:WaitForChild("HumanoidRootPart")
		
	end
	player.CharacterAdded:Connect(function(character)
		local flame = Instance.new("Fire")
		flame.Parent = player.Character:WaitForChild("HumanoidRootPart")
	end)
end

local function onPlayerAdded(player)
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)
	end)

	if not success then
		warn("Error while checking if player had gamepass")
		return
	end

	if hasPass then
		player:SetAttribute("GamePassFire", true)
		giveGamePassPerks(player)
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, wasPurchased)
	if wasPurchased and not player:GetAttribute("GamePassFire") then
		giveGamePassPerks(player)
		player:SetAttribute("GamePassFire", true)
	end
end)

Players.PlayerAdded:Connect(onPlayerAdded)

Maybe someone can help me out here and see what lines of code I’ve got to add to the script? Thank you in advance!

What is the error in the script?

1 Like

There are no errors actually. Like I mentioned before, the script totally works, however I have absolutely no idea what code I’ve got to add in order to give a player tool(s) after they have purchased the gamepass in-game.

FYI, the part where you can add these effects is in this part of the code:

local function giveGamePassPerks(player)
	if player.Character then
		local flame = Instance.new("Fire")
		flame.Parent = player.Character:WaitForChild("HumanoidRootPart")
		
	end
	player.CharacterAdded:Connect(function(character)
		local flame = Instance.new("Fire")
		flame.Parent = player.Character:WaitForChild("HumanoidRootPart")
	end)
end

The “local flame” instance is actually a test effect I added in order to check if the script works, and it does indeed give you the flame effect when you purchase the gamepass in-game (in the test server on Studio). But instead of a flame effect, I want to give the player a few tools.

Store the tools in a storing service, such as ServerStorage. Replace the current giveGamePassPerks function code with this one.

local function giveGamePassPerks(player)
	if player.Character then
	    local ss = game.ServerStorage
        local tool = ss.Tool:Clone()
        tool.Parent = player.Backpack
	end
	player.CharacterAdded:Connect(function(character)
		local flame = Instance.new("Fire")
		flame.Parent = player.Character:WaitForChild("HumanoidRootPart")
	end)
end

The tool(s) will be cloned, and the clone will be added into the player’s inventory.

1 Like

Hey there, thanks for the help. Is there also a way to only clone certain tools? (I’ve currently got a bunch of tools in Rep. storage, obviously I don’t want all of those tools to be given to the user).

You can duplicate these two lines over and over, to add more tools into the inventory.

local tool = ss.Tool:Clone()
tool.Parent = player.Backpack

--local tool1 = ss.Tool1:Clone()
--tool1.Parent = player.Backpack
--local tool2 = ss.Tool2:Clone()
--tool2.Parent = player.Backpack
--local tool3 = ss.Tool3:Clone()
--tool3.Parent = player.Backpack

However, there’s an easier solution, which doesn’t have to do with spamming Ctrl+C and Ctrl+V:

  • Create a folder, and place your tools inside the folder
  • Parent the folder in ReplicatedStorage
  • Using the for i,v in pairs() loop, you can add each tool without duplicating code:
if player.Character then
   local ss = game.ServerStorage
   for i,v in pairs(ss.Folder:GetChildren()) do
      if v:IsA("Tool") then
         v.Parent = player.Backpack
      end
   end
end
1 Like

Alright, and with these lines of code, it would be able to detect which folder I’m referring to…?

I have 2 more gamepasses that would need a handler like this one. If I have multiple folders, the script would need to know which folder it has to work with, right?

Define the other gamepass thats what you can do.

Yes, and you’d have to rename each folder different names, to prevent any errors.

   local ss = game.ServerStorage
for i,v in pairs(ss.Folder1:GetChildren()) do
   --code here
end
for i,v in pairs(ss.Folder2:GetChildren()) do
   --more code here
end
1 Like

Ah okay, I think I get it now. Let me update the script in Studio, test it out, and I’ll come back here with what my findings are.

Alright, tested it out, everything checked out well! Thank you for helping me out with this, I had absolutely no idea what I had to do here.

1 Like

New problem arose unfortunately. When I made a new handler script for a different gamepass, I ran into two issues:

  • When I was testing in a new Studio server, I found that when I bought the first gamepass, I’d get the tools from said gamepass. However when I bought the second gamepass, it wouldn’t give me the tools. I suspect it’s because an attribute from the first gamepass is somehow going beyond its limits and is blocking the second gamepass from giving “multiple copies of a tool”.
  • While I was testing my new scripts a second time, I found that when I bought gamepass 1, it would give me the tools of gamepass 2…

Tried to fix this by renaming GamePassID to GamePassID1 (for anyone asking: the folder of the tools of gamepass2 has a different name yes) to no avail.

Script #1 (gamepass #1):

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

local gamePassId = 25817134

local function giveGamePassPerks(player)
	if player.Character then
		local ss = game.ReplicatedStorage
		for i,v in pairs(ss.GCTools:GetChildren()) do
			if v:IsA("Tool") then
				v.Parent = player.Backpack
			end
		end
	end
	player.CharacterAdded:Connect(function(character)
		local ss = game.ReplicatedStorage
		for i,v in pairs(ss.GCTools:GetChildren()) do
			if v:IsA("Tool") then
				v.Parent = player.Backpack
			end
		end
	end)
end

local function onPlayerAdded(player)
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)
	end)

	if not success then
		warn("Error while checking if player had gamepass")
		return
	end

	if hasPass then
		player:SetAttribute("GamePassFire", true)
		giveGamePassPerks(player)
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, wasPurchased)
	if wasPurchased and not player:GetAttribute("GamePassFire") then
		giveGamePassPerks(player)
		player:SetAttribute("GamePassFire", true)
	end
end)

Players.PlayerAdded:Connect(onPlayerAdded)

Script #2 (gamepass #2

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

local gamePassId1 = 25817172

local function giveGamePassPerks(player)
	if player.Character then
		local ss = game.ReplicatedStorage
		for i,v in pairs(ss.CCTools:GetChildren()) do
			if v:IsA("Tool") then
				v.Parent = player.Backpack
			end
		end
	end
	player.CharacterAdded:Connect(function(character)
		local ss = game.ReplicatedStorage
		for i,v in pairs(ss.CCTools:GetChildren()) do
			if v:IsA("Tool") then
				v.Parent = player.Backpack
			end
		end
	end)
end

local function onPlayerAdded(player)
	local hasPass = false
	
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId1)
	end)

	if not success then
		warn("Error while checking if player had gamepass")
		return
	end

	if hasPass then
		player:SetAttribute("GamePassFire", true)
		giveGamePassPerks(player)
	end
end

MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId1, wasPurchased)
	if wasPurchased and not player:GetAttribute("GamePassFire") then
		giveGamePassPerks(player)
		player:SetAttribute("GamePassFire", true)
	end
end)

Players.PlayerAdded:Connect(onPlayerAdded)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.