Detecting if a player owns a gamepass in a tool

So I’m basically trying to make it so that if a player owns specific gamepasses, the amount of currency (Lazercards) you get from tagging is higher than the default, but it just always gives the base amount, 5. After testing, I noticed that it said that the player owns ALL of the passes, not just the one I specifically had them purchase.

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
	FireAudio:Play()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*50,raycastParams)
	print(rayResult.Position)
	local rayPos = rayResult.Position

	if rayResult then
		local hitPart = rayResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		if model:FindFirstChild("Humanoid") then
			if player.Team ~= game:GetService("Players"):GetPlayerFromCharacter(model).Team then
				if model.Humanoid.Health == 100 then
					local owning1 = pcall(function()
						Owns1 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, secondPassID)
						print("They own 1")
					end)
					local owning2 = pcall(function()
						Owns2 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, secondPassID)
						print("They own 2")
					end)
					local owning3 = pcall(function()
						Owns3 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, thirdPassID)
						print("They own 3")
					end)
					local Player = game:GetService("Players"):GetPlayerFromCharacter(model)
					GotShot:FireClient(Player)
					player.leaderstats.Tags.Value = player.leaderstats.Tags.Value + 1
					if Owns1 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 10
					elseif Owns2 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 15
					elseif Owns3 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 20
					else
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 5
					end
					model.Humanoid.Health -= 5
				end
			end
		end
--unimportant
		local laser = Instance.new("Part")
		laser.Anchored = true
		laser.CanCollide = true
		laser.BrickColor = BrickColor.new("Really red")
		laser.Material = ("Neon")
		laser.Transparency = 1
		laser.Size = Vector3.new(.05,.05,.05)
		laser.Name = ("RedLaser")
		laser.Position = rayPos
		local laserLight = Instance.new("PointLight")
		laserLight.Brightness = 40
		laserLight.Range = .25
		laserLight.Color = Color3.new(1, 0, 0)
		laserLight.Parent = laser
		laser.Parent = game.Workspace
		wait(0.05)
		laser:Destroy()
--unimportant
	end
end)

If more information is needed, feel free to ask me. I will try to reply to people some hours after this was posted.

so the main issue with the script from what ive seen so far would be you are using global variables instead of local ones. thus printing out the statement that “all player owns ALL of the passes
also make sure that " If the game pass is purchased in-game (through PromptGamePassPurchase ), this function may return false due to the caching behavior."
also for players just gaining the default base amount could be due to either sharing of variables or some other feature

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)
	FireAudio:Play()
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local rayResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*50,raycastParams)
	print(rayResult.Position)
	local rayPos = rayResult.Position

	if rayResult then
		local hitPart = rayResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		if model:FindFirstChild("Humanoid") then
			if player.Team ~= game:GetService("Players"):GetPlayerFromCharacter(model).Team then
				if model.Humanoid.Health == 100 then
                    local Owns1, Owns2, Owns3; --since these were global if someone had the gamepass then it wouldve been true for cases when people did not have gamepasses as well
					local owning1 = pcall(function()
						Owns1 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, secondPassID)
						print("They own 1")
					end)
					local owning2 = pcall(function()
						Owns2 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, secondPassID)
						print("They own 2")
					end)
					local owning3 = pcall(function()
						Owns3 = MarketPlaceService:UserOwnsGamePassAsync(player.userId, thirdPassID)
						print("They own 3")
					end)
					local Player = game:GetService("Players"):GetPlayerFromCharacter(model)
					GotShot:FireClient(Player)
					player.leaderstats.Tags.Value = player.leaderstats.Tags.Value + 1
					if Owns1 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 10
					elseif Owns2 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 15
					elseif Owns3 then
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 20
					else
						player.leaderstats.Lazercards.Value = player.leaderstats.Lazercards.Value + 5
					end
					model.Humanoid.Health -= 5
				end
			end
		end
--unimportant
		local laser = Instance.new("Part")
		laser.Anchored = true
		laser.CanCollide = true
		laser.BrickColor = BrickColor.new("Really red")
		laser.Material = ("Neon")
		laser.Transparency = 1
		laser.Size = Vector3.new(.05,.05,.05)
		laser.Name = ("RedLaser")
		laser.Position = rayPos
		local laserLight = Instance.new("PointLight")
		laserLight.Brightness = 40
		laserLight.Range = .25
		laserLight.Color = Color3.new(1, 0, 0)
		laserLight.Parent = laser
		laser.Parent = game.Workspace
		wait(0.05)
		laser:Destroy()
--unimportant
	end
end)
1 Like