Player not being awarded with a certain gear when purchasing a gamepass

I’m working on a gamepass that gives you a gear when purchased, and I’m having trouble with issues from the script and technical bugs.

The first issue comes from the button. When it prompts the gamepass purchase, once it’s finished I get an error says “Pistol3 is not a valid member of game.replicatedfirst”. I also tried putting the tool in workspace and replicatedstorage. The script is a local script inside of a button in the starterGUI. Here’s the script:

local Player = game.Players.LocalPlayer
local MarketPlaceService = game:GetService("MarketplaceService")

local GamePassId = script.Parent.GamePassID.Value
local PurchaseButton = script.Parent

PurchaseButton.MouseButton1Click:Connect(function()
	if MarketPlaceService:UserOwnsGamePassAsync(Player.UserId, GamePassId) then return end
	MarketPlaceService:PromptGamePassPurchase(Player, GamePassId)
	MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function()
		local Gear = game.ReplicatedFirst.Pistol3
		Gear.Parent = game.Players.LocalPlayer.Backpack
	end)
end)

The 2nd script is supposed to give the player the certain gear when he joins the game. The second issue is that the player doesn’t get the gear when he joins the game. The script is a local script inside of StarterPlayerScripts. Here’s the script:

game.Players.PlayerAdded:Connect(function()
	local GamePassID = game.Players.LocalPlayer.PlayerGui:WaitForChild("SideGUI").ShopsFrame.MenuFrame.TestGamepass.GamePassID.Value
	local M = game:GetService("MarketplaceService")
	if M:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, GamePassID) then
		local Gear = game.ReplicatedFirst.Pistol3
		Gear.Parent = game.Players.LocalPlayer.Backpack
	end
end)

If someone can help me, or at least tell me what I was doing wrong, I’d appreciate that! Thanks!

2 Likes

Try put the pistol in ReplicatedStorage where both client and server can access it.

game.Players.PlayerAdded:Connect(function()
	local GamePassID = game.Players.LocalPlayer.PlayerGui:WaitForChild("SideGUI").ShopsFrame.MenuFrame.TestGamepass.GamePassID.Value
	local M = game:GetService("MarketplaceService")
	if M:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, GamePassID) then
		local Gear = game.ReplicatedStorage.Pistol3
		Gear.Parent = game.Players.LocalPlayer.Backpack
	end
end)

So instead off ReplicatedFirst Try replicatedStorage if that dosent work i dont know then.

1 Like

Have you also tried cloning the tool instead of taking it using :Clone()?

Wait have you tried putting both the items in replicatedStorage?
Also make the second script a server script as local scripts will not replicate to the server

2 Likes

I believe PromptGamePassPurchaseFinished can be replicated & detected across both sides of the server/client, plus if you handle the cloning on a LocalScript the Tool itself won’t work well

I’d recommend putting your MarketplaceService Events on the Server-Side instead, and/or fire a RemoteEvent to detect it’s change

Also why are you putting a PlayerAdded event in StarterPlayerScripts…? It should belong as a ServerScript inside ServerScriptService

local Gear = game.ReplicatedFirst:WaitForChild("Pistol3")

game.Players.PlayerAdded:Connect(function(Player)
	local GamePassID = Player.PlayerGui:WaitForChild("SideGUI").ShopsFrame.MenuFrame.TestGamepass.GamePassID.Value
	local M = game:GetService("MarketplaceService")
	if M:UserOwnsGamePassAsync(Player.UserId, GamePassID) then
		local Gear = game.ReplicatedStorage.Pistol3
		Gear.Parent = Player.Backpack
    else
        M:PromptGamePassPurchase(Player, GamePassID)

        M.PromptGamePassPurchaseFinished:Connect(function(PlayerID, GamepassID, WasPurchased)
            if WasPurchased then
                local Clone = Gear:Clone()
                Clone.Parent = Player.Backpack
            end
        end)
	end
end)
2 Likes

You’re right! I’ll try it soon, thanks!

I’ll tell you what problem I’m facing now.

So, the cloned tool only worked on the gamepass purchase, the issue is that the tool doesn’t work when I equip it.

The script in serverscriptservice didn’t work.

Alright.
So by looking at the 2 scripts you originally attached, it is obvious that they are both LocalScriptsimage.
A LocalScript image is a script that runs on the client.
With roblox, you have to deal with replication where basically, if something happens on the server, its visible to everyone, if it happens on the client, it is only visible to the person the script is running on.
imageServerscripts should be used for game logic such as damage, round system, etc.
imageLocalscripts should be used for things that are not neccessary to be done on the server such as visual effects, etc.

In short, what you have done now will not replicate and thats what I assume that you mean that your tools don’t work.

I have made a script that you should put in ServerScriptService image in a Serverscriptimage.

local MarketPlaceService = game:GetService("MarketplaceService")
game.Players.PlayerAdded:Connect(function(plr)
	local playerGui = plr:WaitForChild("PlayerGui")
	local button = playerGui:WaitForChild("SideGUI").ShopsFrame.MenuFrame.TestGamepass
	local gamePassId = button.GamePassID.Value
	
	if MarketPlaceService:UserOwnsGamePassAsync(game.Players.LocalPlayer.UserId, gamePassId) then
		local Gear = game.ReplicatedFirst.Pistol3:Clone()
		Gear.Parent = plr.Backpack
	end
	
	button.MouseButton1Click:Connect(function()
		if MarketPlaceService:UserOwnsGamePassAsync(plr.UserId, gamePassId) then return end
		MarketPlaceService:PromptGamePassPurchase(plr, gamePassId)
		MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function()
			local Gear = game.ReplicatedFirst.Pistol3:Clone()
			Gear.Parent = plr.Backpack
		end)
	end)
end)
2 Likes

let me auto-fix you a bit.

At this line you forgot to replace the local player in the brackets with (plr.UserId, gamePassId)

Even though that worked! Thank you :smiley:

Oh nice catch!
Glad to help. :slight_smile:

@CentiDev When you click the button, it awards the gears to everyone!

Sorry for the late response!
I would taking this part out of the script:

button.MouseButton1Click:Connect(function()
		if MarketPlaceService:UserOwnsGamePassAsync(plr.UserId, gamePassId) then return end
		MarketPlaceService:PromptGamePassPurchase(plr, gamePassId)
		MarketPlaceService.PromptGamePassPurchaseFinished:Connect(function()
			local Gear = game.ReplicatedFirst.Pistol3:Clone()
			Gear.Parent = plr.Backpack
		end)
	end)

And putting it in a serverscript in the button

Also add this before the code:

local plr = script
while True do
if not plr:IsA(“Player”) then
plr = plr.Parent
else
break
end
end

There might be some errors since I typed it up on mobile
Also don’t forget to address marketplaceservice and the button itself

it still awards the gear to all of the players!

I managed to fix this! The other script caused it to clone. Thanks @CentiDev :smiley:

Oh alright, I did find it a bit strange that it cloned it to other players, Glad the issue is fixed now!
(btw is the site looking a bit weird to you too? or just me?


My internet isn’t the slowest but this usually doesn’t happen.

(nevermind the site works again, but it didnt for the past like 20 minutes lol)

oh god. That’s very strange o_O?