RemoteFunction running twice

Hello! I am having an issue where a function is running twice which is making it spend twice as much.

The script that is running the Remote Function:

		if player.Character ~= nil and isHatching == false then
			local nearestEgg
			local plrPos = player.Character.HumanoidRootPart.Position

			for i, v in pairs(Eggs:GetChildren()) do
				if nearestEgg == nil then
					nearestEgg = v
				else
					if (plrPos - v.PrimaryPart.Position).Magnitude < (nearestEgg.PrimaryPart.Position - plrPos).Magnitude then
						nearestEgg = v
					end
				end
			end

			local price = nearestEgg.Price.Value
			local currency = nearestEgg.Currency.Value

			if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) < MaxDisplayDistance then
				canHatch = true
			else
				canHatch = false
			end

			if isCurrentlyAutoHatching == false then
				if canHatch == true then
					isAutoHatching = true
					isCurrentlyAutoHatching = true
					print("Started")
					while isAutoHatching == true do
						print("Looping")
						local result = replicatedStorage:WaitForChild("EggHatchingRemotes"):WaitForChild("AutoHatch"):InvokeServer(nearestEgg)
						local result1, result2, result3 = replicatedStorage.EggHatchingRemotes.AutoHatch:InvokeServer(nearestEgg)

						-- If they do NOT own the gamepass
						if player.Values.CanTripleHatch.Value == false then
							print("test")
							if result ~= "Cannot Afford" then -- If the result is anything but "Cannot Afford"
								if not cooldown then
									cooldown = true
									hatchOne(result, nearestEgg)
									wait(0.5)
									cooldown = false
								end

								if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) > MaxDisplayDistance or player.leaderstats[currency].Value <= price then
									isCurrentlyAutoHatching = false
									print("broke loop")
									break
								end
								-- If they do not afford the egg
							elseif result == "Cannot Afford" then
								print("broke loop (single)")
								isCurrentlyAutoHatching = false
								break
							end

							if player.leaderstats[currency].Value <= price then
								print("broke loop (single)")
								isCurrentlyAutoHatching = false
								break
							end
							
							-- If they own Triple Hatch
						elseif player.Values.CanTripleHatch.Value == true then
							print("test2")
							if result1 ~= "Cannot Afford" and result2 ~= "Cannot Afford" and result3 ~= "Cannot Afford" then
								if not cooldown then
									cooldown = true
									tripleHatch(result1,result2,result3,nearestEgg)
									wait(0.5)
									cooldown = false
								end
								
								if player:DistanceFromCharacter(nearestEgg.EggMesh.PrimaryPart.Position) > MaxDisplayDistance then
									print("broke loop")
									isCurrentlyAutoHatching = false
									break
								end
							-- If they do not afford the egg
							elseif result1 == "Cannot Afford" and result2 == "Cannot Afford" and result3 == "Cannot Afford" then
								print("broke loop (triple)")
								isCurrentlyAutoHatching = false
								break
							end
							
							if player.leaderstats[currency].Value <= price * 3 then
								print("broke loop (triple)")
								isCurrentlyAutoHatching = false
								break
							end
						end
					end
				end
			end
		end

ServerScriptService Script:

game.ReplicatedStorage.EggHatchingRemotes.AutoHatch.OnServerInvoke = function(player, egg)
	local eggModel = workspace:FindFirstChild("Folders"):FindFirstChild("_EGGS"):FindFirstChild(egg.Name)
	if eggModel ~= nil then
		local price = eggModel.Price.Value
		local currency = eggModel.Currency.Value
		local numberPrice = tonumber(price)

		if player.Values.CanTripleHatch.Value == true then -- they DO own the gamepass
			if player.leaderstats[currency].Value >= price * 3 then -- check if they have more than enough or equal too
				player.leaderstats[currency].Value = player.leaderstats[currency].Value - price * 3 -- subtract the price
				
				local chosenPet = choosePet(egg)
				local chosenPet2 = choosePet(egg)
				local chosenPet3 = choosePet(egg)
				
				local val = Instance.new("StringValue")
				val.Name  = chosenPet
				val.Parent = player.Pets

				local val2 = Instance.new("StringValue")
				val2.Name  = chosenPet2
				val2.Parent = player.Pets

				local val3 = Instance.new("StringValue")
				val3.Name  = chosenPet3
				val3.Parent = player.Pets
				
				print("test234890")
				return chosenPet, chosenPet2, chosenPet3
			elseif player.leaderstats[currency].Value <= price * 3 then
				return "Cannot Afford"
			end
			
		elseif player.Values.CanTripleHatch.Value == false then -- they do NOT own the gamepass
			if player.leaderstats[currency].Value >= price then -- check if they have more than enough or equal too
				player.leaderstats[currency].Value = player.leaderstats[currency].Value - price -- subtract the price
				
				local chosenPet = choosePet(egg)
				
				local val = Instance.new("StringValue")
				val.Name  = chosenPet
				val.Parent = player.Pets
				
				return chosenPet
			elseif player.leaderstats[currency].Value <= price then
				return "Cannot Afford"
			end
		end
	end
end

What could be causing this issue? It’s making it spend twice the amount that it should.

I don’t understand why you are actually invoking the server twice? I believe that is the mistake.

Because one result is for singular hatching, and the other is for triple? Which is why one has 3 and the other only has 1.

They are executing exactly the same code, just the fact that you don’t have to invoke the server twice for that. All you have to do is to invoke it once and allow the server to return one or three values. Client should be able to tell if the second and third values are nil or not and do its own operations as indicated.

1 Like

Oh wow, that actually did resolve my issue. I guess I wasn’t thinking when I was doing it. Thank you! I very much appreciate it.

1 Like