Need help with making a 2x currency gamepass

I’m currently trying to get a double rebirths script for an example. I keep finding tutorials on how to make them but they just seem to double my currency everytime I rejoin the game. Here’s the script, please tell me how to fix it. I’m a beginner scripter.

game.Players.PlayerAdded:Connect(function(player)
	
	local marketservice = game:GetService("MarketplaceService")
	
	local can = true
	repeat wait() until player:FindFirstChild("leaderstats")
	local leader = player:FindFirstChild("leaderstats")
	if leader then
	repeat wait() until leader:FindFirstChild(script:WaitForChild("Currency").Value)
	local currency = leader:FindFirstChild(script:WaitForChild("Currency").Value)
		if currency then
			local folder = Instance.new("Folder",player)
			folder.Name = "2xGamepass"
			local oldcash = Instance.new("NumberValue",folder)
			oldcash.Name = "OldCoins"
			oldcash.Value = currency.Value
			local give2x = Instance.new("NumberValue",folder)
			give2x.Name = "Give2x"
			give2x.Value = 0
			
			currency.Changed:Connect(function()
				if marketservice:UserOwnsGamePassAsync(player.UserId, script:WaitForChild("GamepassId").Value) then
				
					
				if can == true then
					can = false
				if currency.Value > oldcash.Value then
				give2x.Value = currency.Value - oldcash.Value
			
				currency.Value = currency.Value + give2x.Value
				
				oldcash.Value = currency.Value
					can = true
					else
						
							oldcash.Value = currency.Value
							can = true
						
						
				end
					
				end
				else
					oldcash.Value = currency.Value
				end
			end)
			
		end
	end
end)

I’m using StringValues for the gamepassID and the Currency I want to use.

2 Likes

I mean, you could go the route of detecting whenever the points change as you’re attempting to do here, however it would probably be more efficient to see if the player owns the gamepass and then award the points whenever the server attempts to award a point. Here is a sample below. I used the example of when a player touches a coin, they get 1 point.

local part = workspace.Coin
local MPS = game:GetService("MarketplaceService")
local gamepassId = 1


local function AwardPoints(hit)
	if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
		local player = game.Players:FindFirstChild(hit.Parent.Name)
		if MPS:UserOwnsGamePassAsync(player.userId, gamepassId) then --wrap in a pcall is probably the cautious thing to do here btw, but you didnt do so in your original script so i left it out.
			player.leaderstats.Coins.Value += 1 * 2 -- multiply by two
		else
			player.leaderstats.Coins.Value += 1 -- if they dont own it just give the base amount
		end
		
	end
end


part.Touched:Connect(AwardPoints)

Although you could go with your original script with some touch ups, I personally feel like you’re overthinking it, it doesn’t have to be as difficult as you’re making it out to be.

Alternatively you could set a boolValue to true on the server if they own the gamepass, and just check if it’s true when they attempt to gain a point to prevent overusage of MPS.

Im still pretty lost. Where would I set that boolValue in the script?

Im just not sure why its giving the player double the amount everytime they leave the game. Goes from 2x to 4x to 8x 16x etc.