How would i combine multiple number values into 1 number value?

So basically I am making a game with pets that have damages. The pets have individual damage values put inside them. However, i want to make it so that when a player has more than 1 pet equipped, the damages from ALL the pets will be the damage output. Any ideas on how to do this?

Script I have:

task.spawn(function()
		while wait(1) do
			if player.Values.SentTo.Value ~= nil then
				local CPPs = PPs[player.Name]
				if #CPPs:GetChildren() > 0 then
					if 	#CPPs:GetChildren() >= 2 then
						local TD = player.Values.TotalDamage
						TD.Value = ...
						local pet = CPPs:FindFirstChildOfClass("Model")
						local dmg = pet.Damage.Value * #CPPs:GetChildren()
							player.Values.SentTo.Value.Health.Value = math.max(player.Values.SentTo.Value.Health.Value - dmg, 0)
							if player.Values.SentTo.Value.Health.Value == 0 then
								remotes.StopDamaging:FireServer()
								destroyDrop(player.Values.SentTo.Value)
					
				end
					elseif #CPPs:GetChildren() == 1 then
						local pet = CPPs:FindFirstChildOfClass("Model")
						local dmg = pet.Damage.Value
							player.Values.SentTo.Value.Health.Value = math.max(player.Values.SentTo.Value.Health.Value - dmg, 0)
							if player.Values.SentTo.Value.Health.Value == 0 then
								remotes.StopDamaging:FireServer()
								destroyDrop(player.Values.SentTo.Value)
						end
					end
				end
			end
		end
	end)
1 Like

wdym by “the damages from ALL the pets will be the damage output.”? like if I had a pet with 10 damage, one with 2 damage, and one with 5 damage, what would it output? 825? 8, 2, and 5, or 15? (from what I can see, I believe you want it to be 15?)

are the damage values consistant? (like do critical hits or anything similar exist?)
if so, you could have a total damage value which is updated everytime the pet or damage value (if lvling up exists) changes, and also upon joining.

from your script, it seems all the pets deal the same damage? is that supposed to happen? if not, you should be looping through the children of CPPs and adding each damage value individually

I took the liberty of cleaning it up a bit using the principles of DRY, early return/break/continue, and removing the special handling of cases depending on the number of pets (either 1, or 2 or more). The init stuff is just how I like to do stuff, you don’t have to if you don’t want:

function init()
	task.spawn(function()
		while wait(1) do
			dealDamage()
		end
	end)	
end

function dealDamage()
	local sentTo = player.Values.SentTo.Value
	local CPPs = PPs[player.Name]

	if sentTo == nil then continue end
	if #CPPs:GetChildren() == 0 then continue end

	local dmg = 0
	for _, CPPpet in CPPs:GetChildren() do
		dmg += CPPpet.Damage.Value
	end

	sentTo.Health.Value = math.max(sentTo.Health.Value - dmg, 0)
	if sentTo.Health.Value == 0 then
		remotes.StopDamaging:FireServer()
		destroyDrop(sentTo)
	end
end

init()

It sums up the damage of each pet. I don’t know how you’ve organized you instance hiarchy so maybe CPPpte.Damage.Value is not the right way to determine the damage of a given pet in CPPs.

so to answer your first question, the pets have damage values inside them, and i want them to combine so that all the values total up to the damage output. for example, if a pet has the damage set to 3, and another has the damage to 5, then the damage output will be 8.

so for the second question, no critical hits exist.

and for the third question, the pets currently deal the same damage, but i want to change that somehow

Then like the other person said, it depends on the way you store which pets each player has equipped.