Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Cancelling out Troop Values?

I have four different values that go into each territory. Red, Blue, Green, and Yellow. They are number values. I need to figure out a way to make the values cancel out eachother if they're on the same territory. For example, if there's a territory with 50 blue troops on it, and I place 20 red troops, it will have 30 blue troops and no red troops left. Or if I place 100 red troops, there will be 50 red troops and no blue troops anymore. The current system I have is really flawed because it will go into negatives since it can't cancel them out correctly.

1 answer

Figured it out myself. And even made it so you can use a strategy. If you have more troops than the territory, you can take it over and still keep all of the existing troops to recruit since you overpower them.

local clickDetector = script.Parent.ClickDetector
local RedTeam = game.Workspace.Start.RedTeam
local BlueTeam = game.Workspace.Start.BlueTeam
local GreenTeam = game.Workspace.Start.GreenTeam
local YellowTeam = game.Workspace.Start.YellowTeam
local RedTroops = game.Workspace.Start.RedTeam.Troops
local BlueTroops = game.Workspace.Start.BlueTeam.Troops
local GreenTroops = game.Workspace.Start.GreenTeam.Troops
local YellowTroops = game.Workspace.Start.YellowTeam.Troops
local Red = script.Red
local Blue = script.Blue
local Green = script.Green
local Yellow = script.Yellow
local MyValue = script.Parent.MyValue
local MyParent = script.Parent

local function onClick(player)
	if player.Name == RedTeam.OwnerName.Value then
		if Blue.Value > 0 then
			if Blue.Value >= RedTroops.Value then 
				Blue.Value = Blue.Value - RedTroops.Value
				RedTroops.Value = 0
			elseif Blue.Value < RedTroops.Value then
				Red.Value = Red.Value + RedTroops.Value + Blue.Value
				RedTroops.Value = 0
				Blue.Value = 0
			end
		elseif Green.Value > 0 then
			if Green.Value >= RedTroops.Value then 
				Green.Value = Green.Value - RedTroops.Value
				RedTroops.Value = 0
			elseif Green.Value < RedTroops.Value then
				Red.Value = Red.Value + RedTroops.Value + Green.Value
				RedTroops.Value = 0
				Green.Value = 0
			end
		elseif Yellow.Value > 0 then
			if Yellow.Value >= RedTroops.Value then 
				Yellow.Value = Yellow.Value - RedTroops.Value
				RedTroops.Value = 0
			elseif Yellow.Value < RedTroops.Value then
				Red.Value = Red.Value + RedTroops.Value + Yellow.Value
				RedTroops.Value = 0
				Yellow.Value = 0
			end
		else
			Red.Value = Red.Value + RedTroops.Value
			RedTroops.Value = 0	
		end
		
	elseif 	player.Name == BlueTeam.OwnerName.Value then
		if Red.Value > 0 then
			if Red.Value >= BlueTroops.Value then 
				Red.Value = Red.Value - BlueTroops.Value
				BlueTroops.Value = 0
			elseif Red.Value < BlueTroops.Value then
				Blue.Value = Blue.Value + BlueTroops.Value + Red.Value
				BlueTroops.Value = 0
				Red.Value = 0
			end
		elseif Green.Value > 0 then
			if Green.Value >= BlueTroops.Value then 
				Green.Value = Green.Value - BlueTroops.Value
				BlueTroops.Value = 0
			elseif Green.Value < BlueTroops.Value then
				Blue.Value = Blue.Value + BlueTroops.Value + Green.Value
				BlueTroops.Value = 0
				Green.Value = 0
			end
		elseif Yellow.Value > 0 then
			if Yellow.Value >= BlueTroops.Value then 
				Yellow.Value = Yellow.Value - BlueTroops.Value
				BlueTroops.Value = 0
			elseif Yellow.Value < BlueTroops.Value then
				Blue.Value = Blue.Value + BlueTroops.Value + Yellow.Value
				BlueTroops.Value = 0
				Yellow.Value = 0
			end
		else
			Blue.Value = Blue.Value + BlueTroops.Value
			BlueTroops.Value = 0	
		end
		
	elseif player.Name == GreenTeam.OwnerName.Value then
		if Red.Value > 0 then
			if Red.Value >= GreenTroops.Value then 
				Red.Value = Red.Value - GreenTroops.Value
				GreenTroops.Value = 0
			elseif Red.Value < GreenTroops.Value then
				Green.Value = Green.Value + GreenTroops.Value + Red.Value
				GreenTroops.Value = 0
				Red.Value = 0
			end
		elseif Blue.Value > 0 then
			if Blue.Value >= GreenTroops.Value then 
				Blue.Value = Blue.Value - GreenTroops.Value
				GreenTroops.Value = 0
			elseif Blue.Value < GreenTroops.Value then
				Green.Value = Green.Value + GreenTroops.Value + Blue.Value
				GreenTroops.Value = 0
				Blue.Value = 0
			end
		elseif Yellow.Value > 0 then
			if Yellow.Value >= GreenTroops.Value then 
				Yellow.Value = Yellow.Value - GreenTroops.Value
				GreenTroops.Value = 0
			elseif Yellow.Value < GreenTroops.Value then
				Green.Value = Green.Value + GreenTroops.Value + Yellow.Value
				GreenTroops.Value = 0
				Yellow.Value = 0
			end
		else
			Green.Value = Green.Value + GreenTroops.Value
			GreenTroops.Value = 0	
		end
		
	elseif player.Name == YellowTeam.OwnerName.Value then
		if Red.Value > 0 then
			if Red.Value >= YellowTroops.Value then 
				Red.Value = Red.Value - YellowTroops.Value
				YellowTroops.Value = 0
			elseif Red.Value < YellowTroops.Value then
				Yellow.Value = Yellow.Value + YellowTroops.Value + Red.Value
				YellowTroops.Value = 0
				Red.Value = 0
			end
		elseif Blue.Value > 0 then
			if Blue.Value >= YellowTroops.Value then 
				Blue.Value = Blue.Value - YellowTroops.Value
				YellowTroops.Value = 0
			elseif Blue.Value < YellowTroops.Value then
				Yellow.Value = Yellow.Value + YellowTroops.Value + Blue.Value
				YellowTroops.Value = 0
				Blue.Value = 0
			end
		elseif Green.Value > 0 then
			if Green.Value >= YellowTroops.Value then 
				Green.Value = Green.Value - YellowTroops.Value
				YellowTroops.Value = 0
			elseif Green.Value < YellowTroops.Value then
				Yellow.Value = Yellow.Value + YellowTroops.Value + Green.Value
				YellowTroops.Value = 0
				Green.Value = 0
			end
		else
			Yellow.Value = Yellow.Value + YellowTroops.Value
			YellowTroops.Value = 0	
		end
		
	end
	
	if Red.Value < 0 then
		Red.Value = 0
	end
	if Blue.Value < 0 then
		Blue.Value = 0
	end
	if Green.Value < 0 then
		Green.Value = 0
	end
	if Yellow.Value < 0 then
		Yellow.Value = 0
	end
	wait(1)
	MyValue.Value = Red.Value + Blue.Value + Green.Value + Yellow.Value
	script.Parent.Transparency = (1 - ((math.log(MyValue.Value))/ 10))	
	print(player.Name)
end

MyValue.Changed:connect(function()
	if Red.Value > 0 then
		script.Parent.BrickColor = BrickColor.new(332)
	elseif Blue.Value > 0 then
		script.Parent.BrickColor = BrickColor.new(1011)
	elseif Green.Value > 0 then
		script.Parent.BrickColor = BrickColor.new(304)
	elseif Yellow.Value > 0 then
		script.Parent.BrickColor = BrickColor.new(1017)
		print(script.Parent.BrickColor)
	end
	local NewSize = (math.log(MyValue.Value)) + 10
	MyParent.Size = Vector3.new(20, NewSize, 20)
end)

clickDetector.MouseClick:connect(onClick)

This discussion is preserved as it was. New answers, comments, and votes are closed.