Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I give the player their gear right after they buy it instead of having to rejoin?

Asked by 2 years ago
Edited 2 years ago

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:

01local MarketPlaceService = game:GetService("MarketplaceService")
02local GamepassID = 55223985
03 
04game.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
11end)

Here is the script that prompts the purchase (I don't know if you will need it):

1local button = script.Parent
2local player = game.Players.LocalPlayer
3local gamePassId = script.Parent.GamepassId
4 
5button.MouseButton1Click:connect(function()
6    if player then
7        game:GetService("MarketplaceService"):PromptGamePassPurchase(player, gamePassId.Value)
8    end
9end)

Edit: I tried putting it inside the ends:

01local MarketPlaceService = game:GetService("MarketplaceService")
02local GamepassID = 55223985 -- The Gamepass ID
03 
04game.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 
11game: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
18end)

I tried putting it outside the ends

01local MarketPlaceService = game:GetService("MarketplaceService")
02local GamepassID = 55223985 -- The Gamepass ID
03 
04game.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
11end)
12 
13game: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
18end)

And I tried putting it in and out of this script too:

1local button = script.Parent
2local player = game.Players.LocalPlayer
3local gamePassId = script.Parent.GamepassId
4 
5button.MouseButton1Click:connect(function()
6    if player then
7        game:GetService("MarketplaceService"):PromptGamePassPurchase(player, gamePassId.Value)
8    end
9end)

2 answers

Log in to vote
0
Answered by
aviel101 165
2 years ago
Edited 2 years ago

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

1game: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
6end)

This script should be in ServerScriptService as a ServerScript:

01local MarketPlaceService = game:GetService("MarketplaceService")
02local GamepassID = 55223985 -- The Gamepass ID
03 
04game.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
09end)
10 
11game: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")
View all 22 lines...
0
I get an error, "MarketplaceService" in line 1 is an unknown global, and if I add local I get an error on the "." in line 1 saying "Expected identifier when parsing expression, got "." BunjiBloxxer 51 — 2y
0
i edited the script it should work now aviel101 165 — 2y
0
It still doesn't work, I don't get any errors but I also don't get the coil when I purchase it. BunjiBloxxer 51 — 2y
0
can you update your script in post with the new script? aviel101 165 — 2y
View all comments (10 more)
0
I edited my post with all the ways I tried to implement the code BunjiBloxxer 51 — 2y
0
use the second script, where it is outside, and print the all values (player, id and success) and tell me what it prints aviel101 165 — 2y
0
It says: GravityCoil is not a valid member of ServerStorage "ServerStorage" I think that was an error I missed BunjiBloxxer 51 — 2y
0
it means that there's no GravityCoil in the ServerStorage aviel101 165 — 2y
0
But there is and it's named GravityCoil BunjiBloxxer 51 — 2y
0
that could mean that this script is in LocalScript while it should be in ServerScript aviel101 165 — 2y
0
The script I edited (the second one listed in the original post) is in starter GUI, Screen GUI, Image button, Local Script. If I change the local script to a server script it doesn't work. BunjiBloxxer 51 — 2y
0
i'm talking about your third script in your post, it should be in a serverscript in serverscriptservice aviel101 165 — 2y
0
It was just in the workspace, but I moved it to serverscriptservice, it still gives me the same error BunjiBloxxer 51 — 2y
0
can you copy the new script and tell me what it prints? aviel101 165 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Here is a script I use for buying a tool with leaderstats:

01player = game.Players.LocalPlayer
02backpack = player.Backpack
03local plr = game.Players.LocalPlayer
04 
05plr: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
14end)

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.

0
Thanks for trying to help, but I'm trying to get it to work with a gamepass, not with leaderstats. BunjiBloxxer 51 — 2y

Answer this question