Different function won't fire when player buys pass in-game

I would like to find out why when the player buys VIP while playing the game, the script doesn’t fire the other function.

The script works fine when someone that already owns the gamepass joins but won’t fire the other event when they buy the gamepass while still playing the game.

I have tried searching the devforum and other sources and have not been able to find the answer to my issue.

Here is the script:

function giveCoinsVIP(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	local OldValue = Coins.Value
	
	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti.Value = 0.5
		XPPremiumMulti.Value = 0.5
	end

	TotalCoinsMulti.Value = 1.5 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1.5 + XPPremiumMulti.Value
	
	Coins.Value += 5 * TotalCoinsMulti.Value
	XP.Value += 5 * TotalXPMulti.Value
end

function giveCoins(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	local OldValue = Coins.Value
	
	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti.Value = 0.5
		XPPremiumMulti.Value = 0.5
	end

	TotalCoinsMulti.Value = 1 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1 + XPPremiumMulti.Value
	
	Coins.Value += 5 * TotalCoinsMulti.Value
	XP.Value += 5 * TotalXPMulti.Value
end

game.Players.PlayerAdded:Connect(function(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local Coins = PlayerStats:WaitForChild('Coins')
	local OldValue = Coins.Value
	
	Coins.Changed:Connect(function(Changed)	
		if OldValue ~= Coins.Value then
			if game:GetService('MarketplaceService'):UserOwnsGamePassAsync(player.UserId,27136394) then
				
				task.wait(2)
				giveCoinsVIP(player)
			else
				
				task.wait(2)
				giveCoins(player)
			end
		end
	end)
end)

Thank you for any help in advance!

First of all when you do this:

1 * .5 = .5

So instead you probably want to change it to

CoinsPremiumMulti.Value = 1.5
XPPremiumMulti.Value = 1.5

You need to hook those functions to the marketplace’s “.PromptGamePassPurchaseFinished” event.

https://developer.roblox.com/en-us/api-reference/event/MarketplaceService/PromptGamePassPurchaseFinished

That problem is already taken care of in these two parts:

	TotalCoinsMulti.Value = 1.5 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1.5 + XPPremiumMulti.Value

and

	TotalCoinsMulti.Value = 1 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1 + XPPremiumMulti.Value

I am having some trouble implementing this, could you provide a example? Edit: nvm that is what the devhub is for.

Ok so this isn’t getting any errors but it’s not changing the amount of coins/xp a player gets. Do you see a reason why?

MarketPlaceService = game:GetService('MarketplaceService')

local function giveCoinsVIP(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	local OldValue = Coins.Value
	
	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti.Value = 0.5
		XPPremiumMulti.Value = 0.5
	end

	TotalCoinsMulti.Value = 1.5 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1.5 + XPPremiumMulti.Value
	
	Coins.Value += 5 * TotalCoinsMulti.Value
	XP.Value += 5 * TotalXPMulti.Value
end

local function giveCoins(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	local OldValue = Coins.Value
	
	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti.Value = 0.5
		XPPremiumMulti.Value = 0.5
	end

	TotalCoinsMulti.Value = 1 + CoinsPremiumMulti.Value
	TotalXPMulti.Value = 1 + XPPremiumMulti.Value
	
	Coins.Value += 5 * TotalCoinsMulti.Value
	XP.Value += 5 * TotalXPMulti.Value
end

game.Players.PlayerAdded:Connect(function(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local Coins = PlayerStats:WaitForChild('Coins')
	local OwnsVIP = PlayerStats:WaitForChild('OwnsVIP')
	local OldValue = Coins.Value
	
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

		OwnsVIP = true
	end
	
	Coins.Changed:Connect(function(Changed)	
		if OldValue ~= Coins.Value then
			if OwnsVIP == true then
				
				task.wait(2)
				giveCoinsVIP(player)
			else
				
				task.wait(2)
				giveCoins(player)
			end
		end
	end)
end)

MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(player, id, purchased)
	if id == 27136394 and purchased then

		local PlayerStats = player:WaitForChild('PlayerStats')
		local OwnsVIP = PlayerStats:WaitForChild('OwnsVIP')

		OwnsVIP = true
	end
end)

Ok I have figured out and solved this issue, here is the code for anyone wondering:

MarketPlaceService = game:GetService('MarketplaceService')

local function giveCoins(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinMultiplier = PlayerStats:WaitForChild('CoinMultiplier')
	local XPMultiplier = PlayerStats:WaitForChild('XPMultiplier')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	
	task.spawn(function() 		
		while true do
			
			TotalCoinsMulti.Value = CoinsVIPMulti.Value + CoinsPremiumMulti.Value + CoinMultiplier.Value
			TotalXPMulti.Value = XPVIPMulti.Value + XPPremiumMulti.Value + XPMultiplier.Value
			
			Coins.Value += 5 * TotalCoinsMulti.Value
			XP.Value += 5 * TotalXPMulti.Value
			task.wait(2)
		end
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	
	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti')
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti')
	
	CoinsPremiumMulti.Value = 0
	XPPremiumMulti.Value = 0
	
	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti.Value = 0.5
		XPPremiumMulti.Value = 0.5
	end
	
	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

		CoinsVIPMulti.Value = 1.5
		XPVIPMulti.Value = 1.5
	else
		
		CoinsVIPMulti.Value = 1
		XPVIPMulti.Value = 1
	end
	
	giveCoins(player)
end)

MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(plr, id, purchased)
	if id == 27136394 and purchased then

		local PlayerStats = plr:WaitForChild('PlayerStats')
		local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
		local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
		
		CoinsVIPMulti.Value = 1.5
		XPVIPMulti.Value = 1.5
	end
end)