I tried creating a gamepass where the player can get x2 coins.But it does not work
Script in SERVERSCRIPTSERVICE
local id = 25844191
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
if purchased and id == ido then
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value*2
end
end)
game.Players.PlayerAdded:Connect(function(plr)
if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, id) then
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value*2
end
end)
Script in THE BUTTON:
local id = 25844191
script.Parent.MouseButton1Click:Connect(function()
game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer, id)
end)
I am unsure, but it looks as if you are just doubling the amount initially and being over with it. If the player joined with 10 coins, they’d get 20 and from there everything would be normal. You have to implement the doubling of earning coins in your other scripts too.
By everywhere, I mean where the player earns money. Currently, if the player has 0 and they buy the gamepass or join with the gamepass, they get 0. If they have 10 and buy the gamepass or join with the gamepass, they have 20. It doesn’t just apply in general, it is like a one-time “double your money” purchase that works every time you join. You would need to add this check wherever someone earns money. Alternatively, you could check how much money the player earned and double it, but I don’t recommend you do that.
Yep. In every script that you have. Otherwise, you could have a system where it checks whenever the money update, and if the value increases, it subtracts from the previous one, gets the difference, and adds it again. If that solved your problem, do mark my reply as a solution. Hopefully I helped.
I don’t see a problem there. He is just checking if the gamepass purchased is the gamepass for x2 money. The problem lies within the code, as it doubles only once and never again.
local mps = game:GetService("MarketplaceService")
local players = game:GetService("Players")
local plr = players.LocalPlayer
local button = script.Parent
local gamepassId = 25844191
button.MouseButton1Click:Connect(function()
mps:PromptGamePassPurchase(plr, gamepassId)
end)
local players = game:GetService("Players")
local mps = game:GetService("MarketplaceService")
local gamepassId = 25844191
mps.PromptGamePassPurchaseFinished:Connect(function(plr, passId, wasPurchased)
if wasPurchased then
if passId == gamepassId then
plr.leaderstats.Coins.Value *= 2
end
end
end)
players.PlayerAdded:Connect(function(plr)
if mps:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
plr.leaderstats.Coins.Value *= 2
end
end)
Remember that when the player joins their stat might be 0 so doubling it will result in the stat still being 0. These scripts will work providing the local script is placed inside the button and the server script is placed somewhere suitable like the ServerScriptService folder.
local players = game:GetService("Players")
local mps = game:GetService("MarketplaceService")
local gamepassId = 25844191
mps.PromptGamePassPurchaseFinished:Connect(function(plr, passId, wasPurchased)
if wasPurchased then
if passId == gamepassId then
plr.leaderstats.Coins.Value *= 2
end
end
end)
players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 100
coins.Parent = leaderstats
if mps:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
plr.leaderstats.Coins.Value *= 2
end
end)
This is a server script with the leaderstats included, when I used it my coin stat was increased from 100 to 200 upon joining the game.
Pretty sure this is the same as his code, just reorganized and more efficient. The gamepass is supposed to give x2 the money you earn, not double it. This doesn’t do much apart from doubling the money whenever someone joins.
Oh, well in that case he’ll need to create a variable/instance value which represents whether or not a player has the gamepass, then whenever the coin stat is increased this can be checked and the coins awarded can be doubled/not doubled accordingly.
If you really don’t want to individually double each value earned, you could add a listener to the leaderstats value and double there. This would look like this:
As an individual script
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local DoubleMoneyGamepassId = 25844191
Players.PlayerAdded:Connect(function(Player)
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, DoubleMoneyGamepassId) then
local Coins = Player:WaitForChild("leaderstats"):WaitForChild("Coins")
local PreviousValue = Coins.Value
Coins.Changed:connect(function()
local Difference = Coins.Value - PreviousValue
if Difference > 0 then
Coins.Value = Coins.Value + Difference
end
end)
end
end)
Inside your leaderstats declaration
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local DoubleMoneyGamepassId = 25844191
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 100 -- However you get your values
Coins.Parent = Leaderstats
if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, DoubleMoneyGamepassId) then
local PreviousValue = Coins.Value
Coins.Changed:connect(function()
local Difference = Coins.Value - PreviousValue
if Difference > 0 then
Coins.Value = Coins.Value + Difference
end
end)
end
end)
The only issue with this script is that it doubles the money EVERY time money are gained, so if someone earns money by killing another player or buys a developer product (such as +1,000 coins), it would double those two. Again, I recommend you go in and do individual checking on each script, but this could work if you don’t care about the two examples above.