So I'm following this tutorial: https://www.youtube.com/watch?v=rLGdpZ8hFkM, when I had it as 1 gamepass, it worked fine, but when I duplicated the code and added new variables, it broke. When you buy both of the gamepasses, the player gets both attributes, which gives both rewards. Any help is much appreciated.
The code related to the gamepass giver:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassId = 20255216
local function giveGolden(player)
--Benefits
if player.Character then
local PlayerGui = player.PlayerGui
PlayerGui:WaitForChild("Poop"):WaitForChild("Poop").Visible = false
PlayerGui:WaitForChild("Poop"):WaitForChild("goldenPoop").Visible = true
end --copy paste benefits into 2nd part
player.CharacterAdded:Connect(function(character)
local PlayerGui = player.PlayerGui
PlayerGui:WaitForChild("Poop"):WaitForChild("Poop").Visible = false
PlayerGui:WaitForChild("Poop"):WaitForChild("goldenPoop").Visible = true
end)
end
local function onPlayerAdded(player)
local hasGold = false
local success, message = pcall(function()
hasGold = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassId)
end)
if not success then
warn("Error while checking if player had gamepass")
return
end
if hasGold then
player:SetAttribute("GamePassFire", true)
giveGolden(player)
end
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player, passId, wasPurchased)
if wasPurchased and not player:GetAttribute("GamePassFire") then
giveGolden(player)
player:SetAttribute("GamePassFire", true)
end
end)
Players.PlayerAdded:Connect(onPlayerAdded)
local gamePassId2 = 20293905
local function giveRainbow(player2)
--Benefits
if player2.Character then
local PlayerGui = player2.PlayerGui
PlayerGui:WaitForChild("Poop"):WaitForChild("Poop").Visible = false
PlayerGui:WaitForChild("Poop"):WaitForChild("rainbowPoop").Visible = true
end --copy paste benefits into 2nd part
player2.CharacterAdded:Connect(function(character)
local PlayerGui = player2.PlayerGui
PlayerGui:WaitForChild("Poop"):WaitForChild("Poop").Visible = false
PlayerGui:WaitForChild("Poop"):WaitForChild("rainbowPoop").Visible = true
end)
end
local function onPlayerAdded2(player2)
local hasRainbow = false
local success2, message2 = pcall(function()
hasRainbow = MarketplaceService:UserOwnsGamePassAsync(player2.UserId, gamePassId2)
end)
if not success2 then
warn("Error while checking if player had gamepass")
return
end
if hasRainbow then
player2:SetAttribute("GamePassFire2", true)
giveRainbow(player2)
end
end
MarketplaceService.PromptGamePassPurchaseFinished:Connect(function(player2, passId2, wasPurchased2)
if wasPurchased2 and not player2:GetAttribute("GamePassFire2") then
giveRainbow(player2)
player2:SetAttribute("GamePassFire2", true)
end
end)
Players.PlayerAdded:Connect(onPlayerAdded2)
