Help with updating gamepass multiplier loop

Try printing the connection when the players buy the gamepass

Both just print ‘Connection’ print(Connection)

oh wait my bad try print(Connection.Connected)

First one prints true second prints false.

So it’s probably the same issue as the debounce so I initialized the variable inside playerAdded and put :UserOwnsGamePassAsync inside of it so it can get the connection value (I had to modify the parameter from player to plr because I have doubt it may get the one from playerAdded which can may cause issues with the script) and passing connection to the doesntHaveVIP function

local RunService = game:GetService("RunService")
local MarketPlaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

function hasVIP(player)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')

	CoinsVIPMulti = 1.5
	XPVIPMulti = 1.5

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end

	local TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	local TotalXPMulti = XPVIPMulti + XPPremiumMulti

	while true do

		Coins.Value += 5 * TotalCoinsMulti
		XP.Value += 5 * TotalXPMulti
		task.wait(2)
	end
end

function doesntHaveVIP(player, Connection)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')
	
	local debounce = false
	
	CoinsVIPMulti = 1
	XPVIPMulti = 1

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end

	local TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	local TotalXPMulti = XPVIPMulti + XPPremiumMulti

	Connection = RunService.Heartbeat:Connect(function()

		if not debounce then	
			debounce = true
			Coins.Value += 5 * TotalCoinsMulti
			XP.Value += 5 * TotalXPMulti
			task.wait(2)
			debounce = false
		end
	end)		
end

players.PlayerAdded:Connect(function(player)

	local Connection

	local PlayerStats = player:WaitForChild('PlayerStats')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')

	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

		hasVIP(player)
	else

		doesntHaveVIP(player, Connection)
	end	

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

When a player purchases VIP now it doesn’t give them any benefits. Also at doesntHaveVIP(player, Connection) It has a orange line under it and says that Connection hasn’t been assigned a variable? Should I change it to something because I don’t think it is passing anything right now. Thanks for all of the help by the way!

1 Like

Try assigning it to nil maybe I think it should accept it

So there are still no errors and everything seems to function properly, but it doesn’t multiply the amount? Do you think it has something to do with this section?

Connection:Disconnect()
hasVIP(plr)

I just noticed it I think it’s because your calling the value instance without putting .Value after it

That doesn’t seem to have fixed the problem either. Here is the current script:

local RunService = game:GetService("RunService")
local MarketPlaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

function hasVIP(player)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti').Value
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti').Value
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti').Value
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti').Value
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti').Value
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti').Value

	CoinsVIPMulti = 1.5
	XPVIPMulti = 1.5

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end
	
	TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	TotalXPMulti = XPVIPMulti + XPPremiumMulti

	while true do

		Coins.Value += 5 * TotalCoinsMulti
		XP.Value += 5 * TotalXPMulti
		task.wait(2)
	end
end

function doesntHaveVIP(player, Connection)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti').Value
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti').Value
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti').Value
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti').Value
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti').Value
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti').Value

	local debounce = false

	CoinsVIPMulti = 1
	XPVIPMulti = 1

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end

	TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	TotalXPMulti = XPVIPMulti + XPPremiumMulti

	Connection = RunService.Heartbeat:Connect(function()

		if not debounce then	
			debounce = true
			Coins.Value += 5 * TotalCoinsMulti
			XP.Value += 5 * TotalXPMulti
			task.wait(2)
			debounce = false
		end
	end)		
end

players.PlayerAdded:Connect(function(player)

	local Connection = nil

	local PlayerStats = player:WaitForChild('PlayerStats')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')

	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

		hasVIP(player)
	else

		doesntHaveVIP(player, Connection)
	end	

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

			Connection:Disconnect()
			hasVIP(plr)
		end
	end)
end)

try putting it on the coins and xp as well

There is a .Value on the coins and xp

Coins.Value += 5 * TotalCoinsMulti
XP.Value += 5 * TotalXPMulti

try printing the multipliers inside the loop of hasVip

So there is now a error at line 104 Connection:Disconnect() so I changed it to Connection:Disconnect(plr) because it was firing the error twice. The error is attempt to index nil with Disconnect.

You shouldn’t pass anything with Disconnected but what did you change? When I told you to print the connection it printed

This doesn’t print anymore but says the same error twice which is attempt to index nil with connected.

could you share the code? This is a kind of weird

Yeah here you go:

local RunService = game:GetService("RunService")
local MarketPlaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")

function hasVIP(player)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti').Value
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti').Value
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti').Value
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti').Value
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti').Value
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti').Value

	CoinsVIPMulti = 1.5
	XPVIPMulti = 1.5

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end

	TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	TotalXPMulti = XPVIPMulti + XPPremiumMulti

	while true do

		Coins.Value += 5 * TotalCoinsMulti
		XP.Value += 5 * TotalXPMulti
		task.wait(2)
	end
end

function doesntHaveVIP(player, Connection)

	local PlayerStats = player:WaitForChild('PlayerStats')
	local XP = PlayerStats:WaitForChild('XP')
	local Coins = PlayerStats:WaitForChild('Coins')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti').Value
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti').Value
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti').Value
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti').Value
	local TotalCoinsMulti = PlayerStats:WaitForChild('TotalCoinsMulti').Value
	local TotalXPMulti = PlayerStats:WaitForChild('TotalXPMulti').Value

	local debounce = false

	CoinsVIPMulti = 1
	XPVIPMulti = 1

	CoinsPremiumMulti = 0
	XPPremiumMulti = 0

	if player.MembershipType == Enum.MembershipType.Premium then

		CoinsPremiumMulti = 0.5
		XPPremiumMulti = 0.5
	end

	TotalCoinsMulti = CoinsVIPMulti + CoinsPremiumMulti
	TotalXPMulti = XPVIPMulti + XPPremiumMulti

	Connection = RunService.Heartbeat:Connect(function()

		if not debounce then	
			debounce = true
			Coins.Value += 5 * TotalCoinsMulti
			XP.Value += 5 * TotalXPMulti
			task.wait(2)
			debounce = false
		end
	end)		
end

players.PlayerAdded:Connect(function(player)

	local Connection = nil

	local PlayerStats = player:WaitForChild('PlayerStats')
	local CoinsVIPMulti = PlayerStats:WaitForChild('CoinsVIPMulti')
	local XPVIPMulti = PlayerStats:WaitForChild('XPVIPMulti')
	local CoinsPremiumMulti = PlayerStats:WaitForChild('CoinsPremiumMulti')
	local XPPremiumMulti = PlayerStats:WaitForChild('XPPremiumMulti')

	if MarketPlaceService:UserOwnsGamePassAsync(player.UserId,27136394) then

		hasVIP(player)
	else

		doesntHaveVIP(player, Connection)
	end	

	MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function(plr, id, purchased)
		if id == 27136394 and purchased then
			
			print(Connection.Connected)
			Connection:Disconnect()
			hasVIP(plr)
		end
	end)
end)

Code runs line by line (unless you create a seperate thread). So even though “hasVIP(player)” is a seperate function outside your PlayerAdded connection, everything inside the hasVIP function will run BEFORE it continues to the next line. So when this happens the script goes line by line and eventually reaches the while loop. This loop will yield the thread indefinitely since its condition is always true.

I wouldn’t recommend running a loop for each player everytime they join but to solve your issue you could simply just use task.spawn so the while loop runs in its own thread.

e.g

task.spawn(function() 
	while true do
		Coins.Value += 5 * TotalCoinsMulti
		XP.Value += 5 * TotalXPMulti
		task.wait(2)
	end
end);

This works but doesn’t solve the problem of the error.