How would I combine multiple seperate number values into 1 damage output?

So basically i am trying to make a game with pets that have individual damage values in them. However, i want those damage values in each pet add up to a total number, which would be the damage output. This would only happen if the player has multiple pets equipped. Any ideas on how to do this?

Script:


local rS = game:GetService("RunService")

local RS = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer

local drops = workspace.MainFolder_Workspace.Drops
local remotes = RS:WaitForChild("Remotes")
local FolderCreate = remotes.FolderCreate
local mainSS = player:WaitForChild("PlayerGui"):WaitForChild("MainSS")
local doorPurchaseUI = mainSS:WaitForChild("PurchaseArea")
local DDs = RS:WaitForChild("DespawnedDrops")
local PPs = workspace.MainFolder_Workspace.PlayerPets
local areaBuyBtnConnection

local function destroyDrop(drop)
	task.wait(0.5)
	local oldParent = drop.Parent
	drop.Parent =  DDs
	task.wait(5)
	drop.Health.Value = drop.MaxHealth.Value
	drop.Parent = oldParent
end

remotes.PromptPurchaseDoor.OnClientEvent:Connect(function(door)
	doorPurchaseUI.Visible = true
	doorPurchaseUI.PurchaseText.Text = "Would you like to purchase this area this area for "..door.Price.Value.." ECoins?"
	
	areaBuyBtnConnection = doorPurchaseUI.Buy.MouseButton1Click:Connect(function()
		local result = remotes.PurchaseDoor:InvokeServer(door)
		if result == true then
			door:Destroy()
		end
		areaBuyBtnConnection:Disconnect()
		doorPurchaseUI.Visible = false
	end)
end)

doorPurchaseUI.Close.MouseButton1Click:Connect(function()
	doorPurchaseUI.Visible = false
end)

	
	for _, drop in pairs(drops:GetDescendants()) do
		if drop:IsA("Model") then
			drop.Health.Value = drop.MaxHealth.Value
			drop.ClickDetector.MouseClick:Connect(function(player)
				local CPPs = PPs[player.Name]
				if #CPPs:GetChildren() > 0 then
					local pet = CPPs:FindFirstChildOfClass("Model")
					if player.Values.SentTo.Value == nil then
						player.Values.SentTo.Value = drop
						pet.Attack.Value = player.Values.SentTo.Value
					elseif player.Values.SentTo.Value == drop then
						player.Values.SentTo.Value = nil
						pet.Attack.Value = player.Values.SentTo.Value
					elseif player.Values.SentTo.Value ~= nil and player.Values.SentTo.Value ~= drop then
						player.Values.SentTo.Value = drop
						pet.Attack.Value = player.Values.SentTo.Value
					end
				end
			end)
		end
	end

	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)


rS.Heartbeat:Connect(function()
	for _, drop in pairs(workspace:WaitForChild("MainFolder_Workspace"):WaitForChild("Drops"):GetDescendants()) do
		if drop:IsA("Model") then
			local hD = drop:FindFirstChild("HealthDisplay")
			local bar = hD.Background.Bar
			local hDT = hD.Background.HealthDisplayLabel
			
			hDT.Text = drop.Health.Value
			bar:TweenSize(UDim2.fromScale(drop.Health.Value/drop.MaxHealth.Value, 1), Enum.EasingDirection.Out, Enum.EasingStyle.Sine, 0.3)
		end
	end
end)

Example of the Pet:
image

make a localscript that stores every pet the player has into a list.

Call a module to do the math on every pet and return you the total damage

if you really just want an idea, make an instance to store the values then have it add on

im not asking for you to make the script for me, but could you give me an idea of what to do? like how would i go about doing that basically

Assuming you have a model containing all of the player’s pets, you can simply loop though each pet, get that pet’s damage amount, then add it to a total value.

local allPets = --pathway to pets folder/model
local totalDamage = 0

for _,v in pairs(allPets:GetChildren()) do
	totalDamage += v.Damage.Value
end

print(totalDamage)
1 Like

i currently do not have something like that
edit: nvm i do

write down a LocalScript that keeps track of every pet the player has using something like :GetPropertyChangedSignal() or just RenderStepped.

this code should send every pet to a table and then you could just directly use it from that code or pass everything to a modulescript

1 Like

thanks for all your help guys! i appreciate it!

2 Likes

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