Script Not detecting which Gamepass

Hi, basically what im trying to do is, I have 2 gamepasses that both give some type of currency multiplier. The script I’m using has both of them in it and is supposed to be able to detect which pass the player has and use that function, and if both passes are owned, then use the third function which adds them. Ive removed a gamepass for testing and it doesnt seem to be detecting which gamepass(es) are owned, sometimes giving a bunch more than intended in the first place. Could someone help out?

Heres the script!

function changeCurrencyMultiplier(newMultiplier, player, Currency, Id)
	local Value = player.leaderstats[Currency].Value
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, Id) then
		while Value > -1 do
			Value = player.leaderstats[Currency].Value
			wait()
			if Value < player.leaderstats[Currency].Value then
				player.leaderstats[Currency].Value += (player.leaderstats[Currency].Value - Value) * newMultiplier
			end
		end
	end

end

game.Players.PlayerAdded:Connect(function(player)
	local Currency = "Coins"
	local Id = 791088881
	local Id2 = 791711505


	local Value = player:WaitForChild("leaderstats")[Currency].Value

	changeCurrencyMultiplier(1, player, Currency, Id)
	changeCurrencyMultiplier(0.5, player, Currency, Id2)
	changeCurrencyMultiplier(1.5, player, Currency, Id, Id2)

end)
1 Like

I can spot a few things that are most likely causing this.
When you run changeCurrencyMultiplier initially with Id1, it will continue listening for changes even if the second invocation, with Id2, overrides it. Further to this, the third time changeCurrencyMultiplier runs, you are passing five arguments when the function only accepts four. Two are gamepass IDs, but the function only checks if they own the first one.

(Side note: are you sure you want the multiplier to be less than 1 if they own a gamepass? Why would someone pay robux to get a 0.5 multiplier?)

The following might fix these issues:

game.Players.PlayerAdded:Connect(function(player)
	local Currency = "Coins"
	local Id = 791088881
	local Id2 = 791711505

	local owns1 = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, Id)
	local owns2 = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.UserId, Id2)

	local multiplier = 1 -- Multiplier if they own neither
	if owns1 and owns2 then
		multiplier = 5 -- Multiplier if they own both
	elseif owns1 then
		multiplier = 2 -- Multiplier if they own ONLY Id1
	elseif owns2 then
		multiplier = 3 -- Multiplier if they own ONLY Id2
	end

	local currencyValue = player:WaitForChild("leaderstats")[Currency]
	local savedValue = currencyValue.Value

	currencyValue:GetPropertyChangedSignal('Value'):Connect(function()
		local diff = currencyValue.Value - savedValue
		if diff < 0 then return end 
		
		diff *= multiplier
		currencyValue.Value = savedValue + diff
		savedValue = currencyValue.Value
	end)
end)

I haven’t tested this code.
You can delete the changeCurrencyMultiplier func also.

1 Like

Oh thank you so much for the help!! I will test it out in a few minutes! And about the 0.5, that was because the script was already doing x1 so it just added them idk its weird…

:joy: It gave this error, and a crap ton of extra coins

Maximum event re-entrancy depth exceeded for Instance.ValueChanged

Edit: Pretty sure thats for an event I have regarding the leaderstats, but it must have done it way too many times. I added a debounce and it worked the first test run but when I went back in again the same thing happened.