Gamepass Script Not Working

Hey Developers!
I am making a story game, I’ve nearly finished. The only part left, is to make the shop gui. I did that fine, and I got to the point where I needed to redeem the items in game. Recently, Roblox removed the thing where you automatically owned Group gamepasses when you made them. So I had to test it like this:

local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')

PlayerService.PlayerAdded:Connect(function(Player)
	print('New Player')
	if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
		game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:WaitForChild('Backpack')
	else
		print('Doesnt Own')
		game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:WaitForChild('Backpack')
	end
end)

Doesn’t Own is printing, but the player isn’t getting the item, I get no errors.
Any help is appreciated!

1 Like

@Little_Joes, look under the second print, that code is the same code that is above “else”]
I don’t know if that is the issue, it probably is.

1 Like

Yes, but it still says Doesnt Own. Which if its a problem, it shouldnt do

1 Like

Hold up, the gamepass isn’t owned and the player doesn’t get the item.
Meaning it prints “doesn’t own”?

1 Like

The player is supposed to get the item, even if they don’t own the Game Pass. The reason they coded like this, is to test it, since they don’t have the Game Pass. They’re not getting the item, is the issue.

1 Like

Yes, I am testing it, I will remove that soon.

1 Like

Wait a bit longer, and check the output for “Infinite yield possible”.
I’ve just replicated this in Roblox Studio, and it’s working fine for me.

1 Like

image
If the instance cannot be found, the script will yield and won’t give the item. Additionally, you could add a timeOut parameter to :WaitForChild(). Do this by adding a second parameter to :WaitForChild().
Example: :WaitForChild(Instance, timeOut)

1 Like

I’ve just compared your code to mine, and I’ve figured out your problem. I replaced :WaitForChild() with :FindFirstChild(), when looking for the players backpack.
Edit: I forgot to add Player:WaitForChild(“Backpack”) in the beginning of the function, excuse me!

local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')

PlayerService.PlayerAdded:Connect(function(Player)
	Player:WaitForChild('Backpack')
	print('New Player')
	if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
		game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
	else
		print('Doesnt Own')
		game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
	end
end)

Now I’ve notcied something weird.
I don’t always get the item, sometimes I do and sometimes I don’t. No errors.

It’s probably an issue where the script loads before the player has fully loaded into the game.

How can I fix this?..

Make it give the tool when the character loads in instead (CharacterAdded function) hopefully that fixes it.

Using Darky’s method, the player will get the item whenever they spawn.
Here’s how your code should look like with this method;

local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')

PlayerService.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		Player:WaitForChild('Backpack')
		print('New Player')
		if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
			game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
		else
			print('Doesnt Own')
			game.ServerStorage:WaitForChild('Assets'):WaitForChild('FlamingRod'):Clone().Parent = Player:FindFirstChild('Backpack')
		end
	end)
end)

Hope this helps!

PlayerAdded function runs before the character spawns, it could happen while the Player is still loading to the game. Character added runs when the Player has finally loaded in the game. This is from my knowledge.

So CharacterAdded should work a lot better here.

CharacterAdded runs when the player spawns. If you set game.Players.CharacterAutoLoads to false, then you will have to manually spawn the player. This is useful for action games, like Phantom Forces, where you click spawn to load into the map.

2 Likes

Thanks for all your help.
@Darkyv_v fixed it with the Character Added Function.
I ended up using this script, and it works perfectly.

local PlayerService = game:GetService('Players')
local MPS = game:GetService('MarketplaceService')
local ServerStorage = game:GetService('ServerStorage')

PlayerService.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function()
		print('New Player')
		if MPS:UserOwnsGamePassAsync(Player.UserId, 8943228) then --For the Flaming Rod Gamepass
			local Rod = game.ServerStorage.Assets.FlamingRod:Clone()
			Rod.Parent = Player.Backpack
		else
			local Rod = game.ServerStorage.Assets.FlamingRod:Clone()
			Rod.Parent = Player.Backpack
		end
		
		if MPS:UserOwnsGamePassAsync(Player.UserId, 8956414) then
			local Flashlight = game.ServerStorage.Assets.Flashlight:Clone()
			Flashlight.Parent = Player.Backpack
		else
			local Flashlight = game.ServerStorage.Assets.Flashlight:Clone()
			Flashlight.Parent = Player.Backpack
		end
		
		if MPS:UserOwnsGamePassAsync(Player.UserId, 8956681) then
			local Medkit = game.ServerStorage.Assets.FirstAid:Clone()
			Medkit.Parent = Player.Backpack
		else
			local Medkit = game.ServerStorage.Assets.FirstAid:Clone()
			Medkit.Parent = Player.Backpack
		end
	end)
end)
1 Like