Double Coin and xp Gamepass

I made a script that is meant to check if a player has two different gamepasses but the script isn’t working. I am pretty new so there is probably a problem with my script that is very easy to fix but I don’t know.

local part = script.Parent

local canGet = true

local MarketplaceService = game:GetService("MarketplaceService")

local DoubleCoingamePassID = 16457854

local DoubleXPgamePassID = 16475164



local function onTouch(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')

	if humanoid then

		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
			
			
		MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, DoubleXPgamePassID, DoubleCoingamePassID, purchaseSuccess)
				
			if  purchaseSuccess == true and DoubleXPgamePassID and DoubleCoingamePassID and player and canGet then

					canGet = false
					
					player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 20 
					
					player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 30 


					wait(0.25)

					canGet = true
					
					part.Touched:Connect(onTouch)
					
else
	
	if purchaseSuccess == true and DoubleXPgamePassID and player and canGet then

					canGet = false

					player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10 

					player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 30 


					wait(0.25)

					canGet = true

					part.Touched:Connect(onTouch)
					
	
	else
									if purchaseSuccess == true and DoubleCoingamePassID and player and canGet then

													canGet = false

													player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 20 

													player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 15 
						
													wait(0.25)

													canGet = true

													part.Touched:Connect(onTouch)	
													
	else
													
													if player and canGet then

																	canGet = false

																	player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10 

																	player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 15 


																	wait(0.25)

																	canGet = true

																	part.Touched:Connect(onTouch)							
																	
																end
															end
														end
													end
												end)
											end
										end

The script is meant to check if a player has two gamepasses and if they have both then it applies both if only one then it applies one and if it doesn’t have any then it leaves it at none

1 Like

There’s so much complexity on this when it can be done way simpler

First a debounce should be encouraged to avoid spam giving, then in the touched event,

  • Get the player
  • Check if a palyer was found and the debounce is not true and continue
  • Set the debounce to true
  • Create the coins and exp variables to give to the player
  • If the player owns either of the gamepass, double the values
  • Add those to the coins and exp leaderstat values

Something like this basically

local part = script.Parent
local canGet = true
local MarketplaceService = game:GetService("MarketplaceService")
local DoubleCoingamePassID = 16457854
local DoubleXPgamePassID = 16475164

local deb = false

part.Touched:Connect(function(hit)
	local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not plr or deb then return end
	
	local lstats = plr.leaderstats
	deb = true
	
	local coins = 10
	local exp = 15
	
	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,DoubleCoingamePassID) then
		coins *= 2
	end
	
	if MarketplaceService:UserOwnsGamePassAsync(plr.UserId,DoubleXPgamePassID) then
		exp *= 2
	end
	
	lstats.Coins.Value += coins
	lstats.Exp.Value += exp
	wait(1)
	deb = false
end)

Also use compound assignments such as += and *= to help reduce clutter if you’re to add something to a value, coins = coins + 1 is the same as coins += 1, but shorter

6 Likes

I put this into my script and it worked but I have a question.

When you do “coins *=2” is that the part where it actually doubles it?

1 Like

Yup, coins *= 2 is a shorthand for coins = coins * 2, if you’re going to do something to the value of the varaible, it’s better to use compound assignments, there’s a lot that exist for each use

+= - Adds a value to a variable, coins += 5 adds 5 to the total in coins
-= - Subtracts a value to a variable, coins -= 5 subtracts 5 to the total in coins
*= - Multiplies by the value given to the variable, coins *= 3 multiplies the total in coins by 3
/= - Divides by the value given to the variable, coins /= 3 divides the total in coins by 3
..= - Concatenates the string given to the variable. Example

local text = "Yes"
text ..= " This is a test"
print(text) -- should print "Yes This is a test" if I'm not mistaken

These can really help if you have long things and you want to add something, so instead of doing

workspace.Folder.Values.Number.Value = workspace.Folder.Values.Number.Value + 5

You can do

workspace.Folder.Values.Number.Value += 5

If you have anymore issues don’t be afraid to make another post!!

2 Likes