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:

local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 55223985 

game.Players.PlayerAdded:Connect(function(player)

    if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then

        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

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

local button = script.Parent
local player = game.Players.LocalPlayer
local gamePassId = script.Parent.GamepassId

button.MouseButton1Click:connect(function()
    if player then
        game:GetService("MarketplaceService"):PromptGamePassPurchase(player, gamePassId.Value)
    end
end)

Edit: I tried putting it inside the ends:

local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 55223985 -- The Gamepass ID

game.Players.PlayerAdded:Connect(function(player)

    if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then

        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, id, success)
    if success and id == 55223985 then
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
            game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
            end
        end)
    end
end)

I tried putting it outside the ends

local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 55223985 -- The Gamepass ID

game.Players.PlayerAdded:Connect(function(player)

    if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then

        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, id, success)
    if success and id == 55223985 then
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

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

local button = script.Parent
local player = game.Players.LocalPlayer
local gamePassId = script.Parent.GamepassId

button.MouseButton1Click:connect(function()
    if player then
        game:GetService("MarketplaceService"):PromptGamePassPurchase(player, gamePassId.Value)
    end
end)

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

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, id, success)
    if success and id == 55223985 then
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
            game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

This script should be in ServerScriptService as a ServerScript:

local MarketPlaceService = game:GetService("MarketplaceService")
local GamepassID = 55223985 -- The Gamepass ID

game.Players.PlayerAdded:Connect(function(player)
    if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, GamepassID) then
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
        game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
    end
end)

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player, id, success)
    local s, r = pcall(function()
        if success and id == 55223985 then
            game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("Backpack")
            game.ServerStorage.GravityCoil:Clone().Parent = player:WaitForChild("StarterGear")
        end
    end)
    if not s then
        print(r)
        print(game.ServerStorage:GetChildren())
    end
end)
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:

player = game.Players.LocalPlayer
backpack = player.Backpack
local plr = game.Players.LocalPlayer

plr:GetMouse().KeyDown:Connect(function(K)
    if K == "q" then
        local sword = game.Lighting.Tools.ClassicSword:Clone() ---- change "ClassicSword" to the name of your tool

        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
            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
            sword.Parent = backpack
        end
    end 
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.

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