Making a premium member get a permanent 1.5x money buff

Title explains it all. But if you really dont get it, hopefully you can make sense by looking at my currently-not-working-jumble-of-mess code

game.Players.PlayerAdded:Connect(function(plr)
	if plr.MembershipType == Enum.MembershipType.Premium then
		local money = plr.leaderstats.Money :: IntValue
		local curMon = money.Value
		money.Changed:Connect(function(newVal)
			local amountAdded = newVal - curMon
			if amountAdded > -1 then
				local addThis = amountAdded / 2
				money.Value += addThis
				print(amountAdded.." in comparison to "..addThis) -- ignore this
			end
		end)
	end
end)
1 Like
game.Players.PlayerAdded:Connect(function(plr)
	if plr.MembershipType == Enum.MembershipType.Premium then
		local money = plr:WaitForChild("leaderstats").Money
		local curMon = money.Value
		money.Changed:Connect(function(newVal)
			local amountAdded = newVal - curMon
			if amountAdded > 0 then
				local addThis = amountAdded / 2
				money.Value += addThis
			end
		end)
	end
end)

Try this. leaderstats doesn’t get added instantly so you should probably wait for it. The :: IntValue prevented it from erroring, if it didn’t.

i just use :: because i like autocorrect, but ill try it RQ

ok well I just tried it and it doesnt work. No errors, no warnings, no nothing

While I’d advice against giving premium multipliers with the intValue.Changed event. I will tell you that you forgot to update curMon whenever money.Changed is fired.

How else would I do it? My code is a pile of garbage anyways so anything else would be helpful. I’ve never really dabbled in robux related programming things.

If you want to give a multiplier to money then you would store a value that is related to the player, called MoneyMultiplier for examle then whenever you intend to add money by some means you would just multiply MoneyMultiplier to that value

Wait, so you’d have to change every money giving script to multiply the money by an intvalue stored somewhere in the player?

Generally you store data like this in a table, but you can create a module script in replicated storage called Calculator and have a function in there that takes a number value and returns the amount you would get after all multipliers are applied to the value, making it so that you don’t have to edit every multiplier when ever you want to make a change. On top of that if you don’t use intValue.Changed you get more control as to when you actually want to give the multiplier to a user.

Would something like this work?

local mkpService = game:GetService("MarketplaceService")
local calculator = {}

local function GetMultiplier(plr: Player)
	local ReturnedTable = {
		["2xMoney"] = {false, 2};
		["Premium"] = {false,1.5}
	}
	if plr.MembershipType == Enum.MembershipType.Premium then
		ReturnedTable.Premium = {true, 2}
	end
	if mkpService:UserOwnsGamePassAsync(plr.UserId,628488827) then
		ReturnedTable["2xMoney"] = {true,1.5}
	end
	if ReturnedTable["2xMoney"][1] and ReturnedTable.Premium[1] then
		return (ReturnedTable["2xMoney"][2]+ReturnedTable.Premium[2])
	end
	if ReturnedTable["2xMoney"][1] then
		return ReturnedTable["2xMoney"][2]
	end
	if ReturnedTable.Premium[1] then
		return ReturnedTable.Premium[2]
	end
	if not ReturnedTable["2xMoney"][1] and not ReturnedTable.Premium[1] then
		return 1
	end
end

function calculator:Calculate(plr: Player,numberAdded: number)
	local Stats = plr.leaderstats
	local Money = Stats.Money
	local Multiplier = GetMultiplier(plr)
	
	Money += (numberAdded * Multiplier)
end

return calculator

I personally would do something like this

local DataModule = -- Module that handles data
local calculator = {}

function calculator.Multiplier(player: Player)
	local multiplier = 1

	if player.MembershipType == Enum.MembershipType.Premium then
		multiplier *= 1.5
	end

	-- Its important to cache the ownership of a gamepass as prompting 
	-- MarketPlaceService is much slower
	if DataModule[player].Gamepasses.DoubleMoney then
		multiplier *= 2
	end

	return multiplier
end

function calculator.MoneyGain(player: Player, money: number)
	local multiplier = calculator.Multiplier(plr)
	local total = money * multiplier

	return total
end

return calculator

Would data module be the module that i use to add leaderstats

(i dont really understand the cache the ownership part

The data module would look something like this:

--[[ Module Script ]]--
local Players = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketPlaceService")

local DataModule = {}

DataModule.PlayerData = {}
DataModule.PlayerCache = {}

function DataModule.CreateEvents()
	Players.PlayerAdded:Connect(function(player)
		DataModule.PlayerData[player] = {
			currency = {
				money = 0,
			},
		}

		DataModule.PlayerCache[player] = { -- Session data, does not save
			gamepasses = {
				DoubleMoney = MarketPlaceService:UserOwnsGamePassAsync(player.UserId, 628488827)
			}
		}
	end)
end

function DataModule.SaveData(player)
	-- Save PlayerData, not cache
end

function DataModule.LoadData(player)
	-- Datastore Load Playerdata
end

return DataModule

But this is just how I structure my code base, you don’t have to do it this way.

I wont do it for the game I’m working on, but this is a good basis for the future, thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.