Please help me with my offered pet 'accept' script

Can someone tell me why every time a player clicks on the accept button the pet will be given one more. So for example a player accepts a trade of 1 pet then he will get 1 pet but if the next player in the server accepts a trade of 1 pet he will get 2 of the same pets with the same values and the next 3 of them.

I understand that this is a long and messy script but can someone explain to me where and why this is happening.

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
	local count = 0
	for i,v in pairs(plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
		if v.Name ~= "UI" then 
			count += 1
		end
	end
	if plr.locked.Value == false and count <= 5 then
	print("fire")
	local p2 = nil
	local lock = false
	local pet
	local pet2 
	local slots = 0
	local countr = 0
	local lock2 = false
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Name == script.Parent.Parent.plr.Value then
			p2 = v
		end
	end
	print("fire2")
	local pets = p2.leaderstats.pets
	print(1)
	if script.Parent.Parent.pet1.Visible == true then
		for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
			if v.Value == 1 then
				print(2)
				pet = v.Value
				print(3)
			end
		end
	end
	if script.Parent.Parent.pet2.Visible == true then
			for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
				if v.Value == 2 then
					print(4)
					pet = v.Value
					print(5)
				end
			end
		end
			if script.Parent.Parent.pet3.Visible == true then
				for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
					if v.Value == 3 then
						print(6)
						pet = v.Value
				print(7)
			end
		end
		end
		print("ee")
		if script.Parent.Parent.pet1.Visible == true then
			print("here")
			if plr.leaderstats.Yums.Value >= script.Parent.Parent.payamt.Value and pet == 1 then
			print("here2")
			print(p2.Name)
			print(p2.Character.Name)
			print(p2.Character.pets.Name)
				for i,v in pairs(p2.Character.pets:GetChildren()) do
					print("here3")
				if v.Name == "Pet1" then
					print("found")
						print(8)
					pet2 = v
					print(v)
					print(pet2)
						print(9)
					end
			end
			print(pet2)
				for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					if v.Name ~= "UI" then
						if lock2 == false and v.Name == "FrameForPet1" then
							v:Destroy()
							print(10)
							lock2 = true
							print(11)
						end
					end
			end
			print(pet2)
			for i,v in pairs(p2.Character.pets:GetChildren()) do
				if pet2 ~= nil then
					if v.number.Value >= pet2.number.Value then
						print(12)
						v.number.Value -= 1
						print(13)
					end
					end
			end
			print(pet2)
				for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
				if pet2 ~= nil then
					if v.Name ~= "UI" then
						print(14)
						if v.number.Value >= pet2.number.Value then
							print(15)
							v.number.Value = v.number.Value - 1 
							print(16)
						end
					end
					end
				end
				print("fire3")
				for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
					v.Value = 0
			end
			if pet2 ~= nil then
			print(pet2)
				pet2:Destroy()
			end
				for i,v in ipairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
				print(v.Name)
				if v.Name ~= "UI" then
					print("oof")
						countr += 1
						warn("count")
						if v.Name == "FrameForPet1" then
							if countr == 1 then
								pets.one.Value = 1
								warn("one")
							else
								if countr == 2 then
									pets.two.Value = 1
								else
									if countr == 3 then
										pets.three.Value = 1
									else
										if countr == 4 then
											pets.four.Value = 1
										else
											if countr == 5 then
												pets.five.Value = 1
											else
												if countr == 6 then
													pets.six.Value = 1
												end
											end
										end
									end
								end
							end
						else

							if v.Name == "FrameForPet2" then
								if countr == 1 then
									warn("one")
									pets.one.Value = 2
								else
									if countr == 2 then
										pets.two.Value = 2
									else
										if countr == 3 then
											pets.three.Value = 2
										else
											if countr == 4 then
												pets.four.Value = 2
											else
												if countr == 5 then
													pets.five.Value = 2
												else
													if countr == 6 then
														pets.six.Value = 2
													end
												end
											end
										end
									end
								end
							else
								if v.Name == "FrameForPet3" then
									if countr == 1 then
										warn("one")
										pets.one.Value = 3
									else
										if countr == 2 then
											pets.two.Value = 3
										else
											if countr == 3 then
												pets.three.Value = 3
											else
												if countr == 4 then
													pets.four.Value = 3
												else
													if countr == 5 then
														pets.five.Value = 3
													else
														if countr == 6 then
															pets.six.Value = 3
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
			end
			print("fire4")
			local pets2 = plr.leaderstats.pets
			plr.leaderstats.Yums.Value -= script.Parent.Parent.payamt.Value
			p2.leaderstats.Yums.Value += script.Parent.Parent.payamt.Value
			for i,v in pairs(plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
				slots += 1
				print(v)
			end
				print("pet")
				if slots == 1 then
					pets2.one.Value = 1
				elseif slots == 2 then
					pets2.two.Value = 1		
				elseif slots == 3 then
					pets2.three.Value = 1	
				elseif slots == 4 then
					pets2.four.Value = 1	
				elseif slots == 5 then
					pets2.five.Value = 1	
				elseif slots == 6 then
					pets2.six.Value = 1	
				end
				local clone = game.ReplicatedStorage.frames.FrameForPet1:Clone()
				clone.Parent = plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames
			clone.number.Value = slots
			
			slots = 0
			
			script.Parent.Parent.Visible = false
			lock = true
			p2.PlayerGui.Offerpet.acceptedoffer.TextLabel.Text = plr.Name.." accepted the offer from you!"
			p2.PlayerGui.Offerpet.acceptedoffer.Visible = true
		else
			if lock ~= true then
			print("fire5")
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = true
			wait(3)
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = false
			print("fire6")
			end
		end
	end
	if script.Parent.Parent.pet2.Visible == true then
		print("here")
		if plr.leaderstats.Yums.Value >= script.Parent.Parent.payamt.Value and pet == 2 then
			print("here2")
			print(p2.Name)
			print(p2.Character.Name)
			print(p2.Character.pets.Name)
			for i,v in pairs(p2.Character.pets:GetChildren()) do
				print("here3")
				if v.Name == "Pet2" then
					print("found")
					print(8)
					pet2 = v
					print(v)
					print(pet2)
					print(9)
				end
			end
			print(pet2)
			for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
				if v.Name ~= "UI" then
					if lock2 == false and v.Name == "FrameForPet2" then
						v:Destroy()
						print(10)
						lock2 = true
						print(11)
					end
				end
			end
			print(pet2)
			for i,v in pairs(p2.Character.pets:GetChildren()) do
				if pet2 ~= nil then
					if v.number.Value >= pet2.number.Value then
						print(12)
						v.number.Value -= 1
						print(13)
					end
					end
				end
				print(pet2)
				for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					if pet2 ~= nil then
						if v.Name ~= "UI" then
							print(14)
							if v.number.Value >= pet2.number.Value then
								print(15)
								v.number.Value = v.number.Value - 1 
								print(16)
							end
						end
					end
				end
				print("fire3")
				for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
					v.Value = 0
				end
				if pet2 ~= nil then
					print(pet2)
					pet2:Destroy()
				end
				for i,v in ipairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					print(v.Name)
					if v.Name ~= "UI" then
						print("oof")
						countr += 1
						warn("count")
						if v.Name == "FrameForPet1" then
							if countr == 1 then
								pets.one.Value = 1
								warn("one")
							else
								if countr == 2 then
									pets.two.Value = 1
								else
									if countr == 3 then
										pets.three.Value = 1
									else
										if countr == 4 then
											pets.four.Value = 1
										else
											if countr == 5 then
												pets.five.Value = 1
											else
												if countr == 6 then
													pets.six.Value = 1
												end
											end
										end
									end
								end
							end
						else

							if v.Name == "FrameForPet2" then
								if countr == 1 then
									warn("one")
									pets.one.Value = 2
								else
									if countr == 2 then
										pets.two.Value = 2
									else
										if countr == 3 then
											pets.three.Value = 2
										else
											if countr == 4 then
												pets.four.Value = 2
											else								--pets
												if countr == 5 then
													pets.five.Value = 2
												else
													if countr == 6 then
														pets.six.Value = 2
													end
												end
											end
										end
									end
								end
							else
								if v.Name == "FrameForPet3" then
									if countr == 1 then
										warn("one")
										pets.one.Value = 3
									else
										if countr == 2 then
											pets.two.Value = 3
										else
											if countr == 3 then
												pets.three.Value = 3
											else
												if countr == 4 then
													pets.four.Value = 3
												else
													if countr == 5 then
														pets.five.Value = 3
													else
														if countr == 6 then
															pets.six.Value = 3
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
				print("fire4")
				local pets2 = plr.leaderstats.pets
				
				plr.leaderstats.Yums.Value -= script.Parent.Parent.payamt.Value
				p2.leaderstats.Yums.Value += script.Parent.Parent.payamt.Value
				
				for i,v in pairs(plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					slots += 1
					print(v)
				end
				print("pet")
				if slots == 1 then
					pets2.one.Value = 2
				elseif slots == 2 then
					pets2.two.Value = 2	
				elseif slots == 3 then
					pets2.three.Value = 2		--slots
				elseif slots == 4 then
					pets2.four.Value = 2	
				elseif slots == 5 then
					pets2.five.Value = 2	
				elseif slots == 6 then
					pets2.six.Value = 2
				end
				local clone = game.ReplicatedStorage.frames.FrameForPet2:Clone()
				clone.Parent = plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames
				clone.number.Value = slots
				slots = 0
				
				print(plr.leaderstats.Yums.Value)
				
				print(pet)
				
				print(script.Parent.Parent.payamt.Value)
				lock = true
				script.Parent.Parent.Visible = false
				p2.PlayerGui.Offerpet.acceptedoffer.TextLabel.Text = plr.Name.." accepted the offer from you!"
				p2.PlayerGui.Offerpet.acceptedoffer.Visible = true
		else
			if lock ~= true then
			print("fire5")
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = true
			wait(3)
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = false
			print("fire6")
			end
		end
	end
	if script.Parent.Parent.pet3.Visible == true then
		print("here")
		if plr.leaderstats.Yums.Value >= script.Parent.Parent.payamt.Value and pet == 3 then
			print("here2")
			print(p2.Name)
			
			print(p2.Character.Name)
			
			print(p2.Character.pets.Name)
			
			for i,v in pairs(p2.Character.pets:GetChildren()) do
				print("here3")
				if v.Name == "Pet3" then
					print("found")
					print(8)					--frames
					pet2 = v
					print(v)
					print(pet2)
					print(9)
				end
			end
			print(pet2)
			for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
				if v.Name ~= "UI" then
					if lock2 == false and v.Name == "FrameForPet3" then
						v:Destroy()
						print(10)
						lock2 = true
						print(11)
					end
				end
			end
			print(pet2)
			for i,v in pairs(p2.Character.pets:GetChildren()) do
				if pet2 ~= nil then
					if v.number.Value >= pet2.number.Value then
						print(12)
						v.number.Value -= 1
						print(13)
					end
				  end
				end
				print(pet2)
				for i,v in pairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					if pet2 ~= nil then
						if v.Name ~= "UI" then
							print(14)
							if v.number.Value >= pet2.number.Value then
								print(15)
								v.number.Value = v.number.Value - 1 
								print(16)
							end
						end
					end
				end
				print("fire3")
				for i,v in pairs(p2.leaderstats.pets:GetChildren()) do
					v.Value = 0
				end
				if pet2 ~= nil then
					print(pet2)
					pet2:Destroy()
				end
				for i,v in ipairs(p2.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					print(v.Name)
					if v.Name ~= "UI" then
						print("oof")
						
						countr += 1
						
						warn("count")
						if v.Name == "FrameForPet1" then
							if countr == 1 then
								pets.one.Value = 1
								warn("one")
							else
								if countr == 2 then
									pets.two.Value = 1
								else
									if countr == 3 then
										pets.three.Value = 1
									else
										if countr == 4 then
											pets.four.Value = 1
										else
											if countr == 5 then
												pets.five.Value = 1
											else
												if countr == 6 then
													pets.six.Value = 1
												end
											end
										end
									end
								end
							end
						else

							if v.Name == "FrameForPet2" then
								if countr == 1 then
									warn("one")
									pets.one.Value = 2
								else
									if countr == 2 then
										pets.two.Value = 2
									else
										if countr == 3 then
											pets.three.Value = 2
										else
											if countr == 4 then
												pets.four.Value = 2
											else
												if countr == 5 then
													pets.five.Value = 2
												else
													if countr == 6 then
														pets.six.Value = 2
													end
												end
											end
										end
									end
								end
							else
								if v.Name == "FrameForPet3" then
									if countr == 1 then
										warn("one")
										pets.one.Value = 3
									else
										if countr == 2 then
											pets.two.Value = 3
										else
											if countr == 3 then
												pets.three.Value = 3
											else
												if countr == 4 then
													pets.four.Value = 3
												else
													if countr == 5 then
														pets.five.Value = 3
													else
														if countr == 6 then
															pets.six.Value = 3
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
				print("fire4")
				local pets2 = plr.leaderstats.pets
				
				plr.leaderstats.Yums.Value -= script.Parent.Parent.payamt.Value
				p2.leaderstats.Yums.Value += script.Parent.Parent.payamt.Value
				
				for i,v in pairs(plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames:GetChildren()) do
					slots += 1
					print(v)
				end
				print("pet")
				if slots == 1 then
					pets2.one.Value = 3
				elseif slots == 2 then
					pets2.two.Value = 3	
				elseif slots == 3 then
					pets2.three.Value = 3
				elseif slots == 4 then
					pets2.four.Value = 3
				elseif slots == 5 then
					pets2.five.Value = 3	
				elseif slots == 6 then
					pets2.six.Value = 3	
				end
				local clone = game.ReplicatedStorage.frames.FrameForPet3:Clone()
				clone.Parent = plr.PlayerGui.PetInv.TextButton.Frame.ScrollingFrame.Frames
				clone.number.Value = slots
				
				slots = 0
				print(plr.leaderstats.Yums.Value)
				
				print(pet)
				
				print(script.Parent.Parent.payamt.Value)
				lock = true
				script.Parent.Parent.Visible = false
				p2.PlayerGui.Offerpet.acceptedoffer.TextLabel.Text = plr.Name.." accepted the offer from you!"
				p2.PlayerGui.Offerpet.acceptedoffer.Visible = true
		else
			if lock ~= true and count <= 6 then
			print("fire5")
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = true
			wait(3)
			plr.PlayerGui.petinvfullorpetequipfull.TextLabel2.Visible = false
			print("fire6")
			end
		end
		lock = false
			print(count)
		end
	else
		if count == 6 then
				plr.PlayerGui.petinvfullorpetequipfull.TextLabel8.Visible = true
				wait(3)
				plr.PlayerGui.petinvfullorpetequipfull.TextLabel8.Visible = false	
		end
		end
end)

Do you realize this is extremely long, messy and probably slow code? You should really look into modules. You don’t have to do everything at once. Your problem is probably really simple, you just have to clean up your room to find what you’re looking for. Cleaner code is easier to debug.

4 Likes

Above :point_up_2: my script I said that it was long and messy, so I completely understand that. If that’s reason why the bug is happening. I made that script long ago btw.

Thx for your reply

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.