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.