Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Gamepass script not giving gear to character once they reset/die?

I have a script that gives a gear to anyone that owns the gamepass:

local devidl = 0000000 --- Gamepass ID
game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(player,gamepassid,ispurchased)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId,devidl) == false then
		if ispurchased == false then
			if gamepassid == devidl then
				local BackpackItem = game.ReplicatedStorage.GravityCoil:clone() 
				local StarterGearItem = game.ReplicatedStorage.GravityCoil:clone()
				BackpackItem.Parent = player.Backpack
				StarterGearItem.Parent = player.StarterGear
			end
		end
	end
end)

game.Players.PlayerAdded:connect(function(player)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(player.userId,devidl) == true then
		local BackpackItem = game.ReplicatedStorage.GravityCoil:Clone()
		local StarterGearItem = game.ReplicatedStorage.GravityCoil:Clone()
		BackpackItem.Parent	= player.Backpack
		StarterGearItem.Parent = player.Backpack
	end
end)

The problem is whenever the character dies, they no longer have the gear. Does anyone know how to fix this?

2 answers

PlayerAdded only fires when a player joins the game. CharacterAdded, however, will fire whenever their character loads, such as after they die. So to solve this, you can add the CharacterAdded event inside your PlayerAdded one:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--Check if user owns gamepass here
	end)
end)

As well as this, you should try make more variables in the future to limit the amount of code you repeat. Such as,

local Market = game:GetService("MarketplaceService")

so that you only have to write it once.

Furthermore, please format your code in code blocks on here in the future (the Lua symbol) before posting.

Hope this helps!

EDIT: Didn't realise StarterGear was a thing, just put the tools inside that instead like incapaz suggested.

Line 20. Change player.Backpack to player.StarterGear and please don’t use deprecated code.

This discussion is preserved as it was. New answers, comments, and votes are closed.