Why does every player in a server get a pet after one player buys a game pass?

So i wrote this code: When a player buys a gamepass, he should get a pet. But for some reason every player who is in the server gets the pet after the gamepass was bought. I have no idea why this happens. Here is the code that i already have:

local id = 9566580

game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,idl,purchased)
 if purchased and idl == id then
	
			print("GamepassBought")
	
 		if plr then
  				local character = plr.Character
  				if character then
   				local humRootPart = character.HumanoidRootPart
  				local newPet = pet:Clone ()

				newPet.Overhead.TextLabel.Text = plr.Name .. "'s Pet"

   				newPet.Parent = character
				newPet.Anchored = false
				newPet.CanCollide = false
   
   				local bodyPos = Instance.new("BodyPosition", newPet)
  				bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
   
   				local bodyGyro = Instance.new("BodyGyro", newPet)
				bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
   
   				while wait() do
   				 bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
   				 bodyGyro.CFrame = humRootPart.CFrame
				end


		end

			end
			
		end	
				
			end)
		
end)
1 Like

No idea. Does the gamepads script only print “GamepassBought” once? If so, it’s probably something else causing the error.

Something unrelated I notice, though, is this:

  				while wait() do
   				 bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
   				 bodyGyro.CFrame = humRootPart.CFrame
				end

That’s going to keep your server script in a loop which you dont want to do. Instead, add a script into the Pet itself which does this and perhaps add an ObjectValue to the pet with the player’s HumanoidRootPart so that the pet can still access the “humRootPart.Position” thing

1 Like

“GamepassBought” was only printed once, which is also the reason for my confusion.
I will definitely insert the while loop into the pet as a separate script.

But I thought that with “(function (plr, idl, purchased)” the player can already be identified, that’s why the repeated duplication makes no sense to me.

But thanks anyway for your help!

Right, there is no reason that pets should go to all players based on your script above. Perhaps this is somehow being caused by another script?

I’ve already thought of that. I only have one server script which manages the gamepass purchase. I have no other ones that can cause these problems.

I notice you have two function-closing ends. This tells me that this PromptGamepassPurchaseFinish script is wrapped inside another function. Is that the case, and if so, could this be causing the issue?

1 Like

Thank you again for helping!

I searched my whole explorer again and found a script somewhere that spawned the pets. Apparently I forgot to start a query there whether the player has the gamepass and that’s why there was spawned a pet for every player.

And like you said one function was inside another one which also caused problems.

1 Like