I have a gamepass that gives the player a gravity coil when they buy it, but they have to rejoin to get it. How do I make it so that they get it right after they purchase it? Here is the script in Workspace that gives them the gear upon joining:
01 | local MarketPlaceService = game:GetService( "MarketplaceService" ) |
02 | local GamepassID = 55223985 |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 |
06 | if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then |
07 |
08 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
09 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
10 | end |
11 | end ) |
Here is the script that prompts the purchase (I don't know if you will need it):
1 | local button = script.Parent |
2 | local player = game.Players.LocalPlayer |
3 | local gamePassId = script.Parent.GamepassId |
4 |
5 | button.MouseButton 1 Click:connect( function () |
6 | if player then |
7 | game:GetService( "MarketplaceService" ):PromptGamePassPurchase(player, gamePassId.Value) |
8 | end |
9 | end ) |
Edit: I tried putting it inside the ends:
01 | local MarketPlaceService = game:GetService( "MarketplaceService" ) |
02 | local GamepassID = 55223985 -- The Gamepass ID |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 |
06 | if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then |
07 |
08 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
09 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
10 |
11 | game:GetService( "MarketplaceService" ).PromptGamePassPurchaseFinished:Connect( function (player, id, success) |
12 | if success and id = = 55223985 then |
13 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
14 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
15 | end |
16 | end ) |
17 | end |
18 | end ) |
I tried putting it outside the ends
01 | local MarketPlaceService = game:GetService( "MarketplaceService" ) |
02 | local GamepassID = 55223985 -- The Gamepass ID |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 |
06 | if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then |
07 |
08 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
09 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
10 | end |
11 | end ) |
12 |
13 | game:GetService( "MarketplaceService" ).PromptGamePassPurchaseFinished:Connect( function (player, id, success) |
14 | if success and id = = 55223985 then |
15 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
16 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
17 | end |
18 | end ) |
And I tried putting it in and out of this script too:
1 | local button = script.Parent |
2 | local player = game.Players.LocalPlayer |
3 | local gamePassId = script.Parent.GamepassId |
4 |
5 | button.MouseButton 1 Click:connect( function () |
6 | if player then |
7 | game:GetService( "MarketplaceService" ):PromptGamePassPurchase(player, gamePassId.Value) |
8 | end |
9 | end ) |
there's something called PromptGamePassPurchaseFinished
, which is an event that fires when a gamepass purchase prompt closes. the success
tells you if the player bought the gamepass successfully, and the id
tells you the gamepass they purchased
just add it to one of your scripts
1 | game:GetService( "MarketplaceService" ).PromptGamePassPurchaseFinished:Connect( function (player, id, success) |
2 | if success and id = = 55223985 then |
3 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
4 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
5 | end |
6 | end ) |
This script should be in ServerScriptService as a ServerScript:
01 | local MarketPlaceService = game:GetService( "MarketplaceService" ) |
02 | local GamepassID = 55223985 -- The Gamepass ID |
03 |
04 | game.Players.PlayerAdded:Connect( function (player) |
05 | if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then |
06 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
07 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
08 | end |
09 | end ) |
10 |
11 | game:GetService( "MarketplaceService" ).PromptGamePassPurchaseFinished:Connect( function (player, id, success) |
12 | local s, r = pcall ( function () |
13 | if success and id = = 55223985 then |
14 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "Backpack" ) |
15 | game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild( "StarterGear" ) |
Here is a script I use for buying a tool with leaderstats:
01 | player = game.Players.LocalPlayer |
02 | backpack = player.Backpack |
03 | local plr = game.Players.LocalPlayer |
04 |
05 | plr:GetMouse().KeyDown:Connect( function (K) |
06 | if K = = "q" then |
07 | local sword = game.Lighting.Tools.ClassicSword:Clone() ---- change "ClassicSword" to the name of your tool |
08 |
09 | if player.leaderstats.Kills.Value > = 15 then ----- change 10 to the price of your tool and "Money" to whatever you named your Points for leaderboard |
10 | player.leaderstats.Kills.Value = player.leaderstats.Kills.Value - 15 ----- change 10 to the price of your tool and "Money" to whatever you named your Points for leaderboard |
11 | sword.Parent = backpack |
12 | end |
13 | end |
14 | end ) |
I put the tool under lighting in a folder named tools. It moves it to the players backpack. If this is any help, let me know. If not, then idk. I am new to scripting.