As the title suggests, how do I make it so that the player receives their gamepass as soon as they buy it? Without the need to leave and rejoining again. Thanks.
Gamepass Script:
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 25805641 -- Change this to your game pass ID
local function onPlayerAdded(player)
local hasPass = false
-- Check if the player already owns the game pass
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass == true then
print(player.Name .. " owns the game pass with ID " .. gamePassID)
player.CharacterAdded:Connect(function(char)
local head = char:FindFirstChild("Head")
local particlesClone = game.ServerStorage.SquidHeads:Clone()
particlesClone.Parent = head
end)
-- Assign this player the ability or bonus related to the game pass
--
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Button Script:
local gamepass_id = 25805641
script.Parent.MouseButton1Click:Connect(function()
game:GetService("MarketplaceService"):PromptGamePassPurchase(game.Players.LocalPlayer, gamepass_id)
end)
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local gamePassID = 25805641 -- Change this to your game pass ID
Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
local hasPass = false
-- Check if the player already owns the game pass
local success, message = pcall(function()
hasPass = MarketplaceService:UserOwnsGamePassAsync(Player.UserId, gamePassID)
end)
-- If there's an error, issue a warning and exit the function
if not success then
warn("Error while checking if player has pass: " .. tostring(message))
return
end
if hasPass then
print(Player.Name .. " owns the game pass with ID " .. gamePassID)
local head = Character:FindFirstChild("Head")
local particlesClone = game.ServerStorage.SquidHeads:Clone()
particlesClone.Parent = head
end
end)
end)
Wrap everything inside a CharacterAdded event instead of a PlayerAdded event, that way the callback function will be executed each time the player’s character spawns/reloads as opposed to when the player first joins the server.
You may want to reload/respawn the player’s character upon a successful purchase of the gamepass from the local script.