Combat Loop Review

When the pet is near a NPC it fires “Start” , if the pet is released it fires “Release”. Is there a better/more efficient way to write this?

local PetInfo = {}
game.ReplicatedStorage.CombatRemote.OnServerEvent:Connect(function(plr,petNum,arg,Target)
	local Pet = GlobalF:GetPetFromNum(plr,petNum)
	if arg == "Start" then
		PetInfo[Pet] = {CurrentCombo = 0}
		Combat(Pet)
	elseif arg == "Release" then
		PetInfo[Pet] = nil
	end
end)

function Combat(Pet)
	if PetInfo[Pet] then
		local Data = PetData[Pet.SourceName.Value]
		if PetInfo[Pet].CurrentCombo < Data.HitsBeforeUlt then
			PetInfo[Pet].CurrentCombo += 1
			print(PetInfo[Pet].CurrentCombo)
			wait(1)
			Combat(Pet)
		else
			Ultimate(Pet)
		end
	end
end

function Ultimate(Pet)
	local Data = PetData[Pet.SourceName.Value]
	print("ULTIII",PetInfo[Pet].CurrentCombo)
	wait(5)
	PetInfo[Pet].CurrentCombo = 0
	Combat(Pet)
end
  1. Is arg variable always equal to "Start" or "Release"? Depending on the answer elseif might get replaced with else and the variable might get changed to boolean.
  2. You used the old and well known wait. I name it “a half-deprecated function”. Consider replacing it with fresher task.wait.
    Anything else seems good to me.
1 Like

I’m using this script to handle all client inputs so the arguments will be different, but the task.wait looks very promising , Thanks!