Basically, this makes it so a flag can be captured by either team, and the GUI above it displays how much of it has been captured so far, and when it's taken the flag turns to the color of the team. The script works perfectly, yet occasionally the bar above will completely freeze and remain black, and the flag itself cannot be reclaimed by either team, and it'll not be able to be captured for the rest of the server lifetime. Any ideas on what I did wrong?

Colors={"Dark green", "Brick yellow"}
Radius=15

function playertotal()
	local a,b=0,0
	for i, x in pairs(game.Players:GetPlayers())do
		pcall(function()
			if x.Character.Humanoid.Health>0 and (x.Character:GetModelCFrame().p-script.Parent.Parent.Base.Position).magnitude<Radius then
				if x.TeamColor==BrickColor.new(Colors[1]) then
					a=a+1
				elseif x.TeamColor==BrickColor.new(Colors[2]) then
					b=b+1
				end
			end
		end)
	end
	return a,b
end

function flagtick()
	if script.Parent.Capture.Value>=100 then
		script.Parent.Capture.Value=0
		if script.Parent.Owner.Value==BrickColor.new(Colors[1]) then
			script.Parent.Owner.Value=BrickColor.new(Colors[2])
		elseif script.Parent.Owner.Value==BrickColor.new(Colors[2]) then
			script.Parent.Owner.Value=BrickColor.new(Colors[1])
		else
			local a,b=playertotal()
			script.Parent.Owner.Value=BrickColor.new(a>b and Colors[1] or a<b and Colors[2])			
		end
	end
	
	local a,b=playertotal()
	if (a>b and script.Parent.Owner.Value~=BrickColor.new(Colors[1])) or (b>a and script.Parent.Owner.Value~=BrickColor.new(Colors[2])) then
		script.Parent.Capture.Value=script.Parent.Capture.Value+1
	end
	if ((a>b and script.Parent.Owner.Value==BrickColor.new(Colors[1])) or (b>a and script.Parent.Owner.Value==BrickColor.new(Colors[2]))) and script.Parent.Capture.Value>0 then
		script.Parent.Capture.Value=script.Parent.Capture.Value-1
	end
	
	script.Parent.BrickColor=script.Parent.Owner.Value
	for i, x in pairs(script.Parent.Parent.Sandbags:children()) do
		x.BrickColor=script.Parent.Owner.Value
	end
	
	script.Parent.BillboardGui.Frame.BackgroundColor3=Color3.new(script.Parent.Owner.Value.Color.r-.2,script.Parent.Owner.Value.Color.g-.2,script.Parent.Owner.Value.Color.b-.2)
	script.Parent.BillboardGui.Frame.Frame.BackgroundColor3=script.Parent.Owner.Value.Color
	
	script.Parent.BillboardGui.Frame.Frame.Frame:TweenSize(UDim2.new(script.Parent.Capture.Value/100,0,1,0),"Out","Linear",.2,true)
	script.Parent.BillboardGui.Frame.Frame.Frame.BackgroundColor3=(script.Parent.Owner.Value==BrickColor.new(Colors[1])) and BrickColor.new(Colors[1]).Color or (script.Parent.Owner.Value==BrickColor.new(Colors[2])) and BrickColor.new(Colors[2]).Color or BrickColor.new("Black").Color
end
script.Parent.BillboardGui.Adornee=script.Parent
while wait(.1) do
	flagtick()
end

3 comments

0

My bad, I realize that a majority was cut out. The entirety of the code is here, http://pastebin.com/8gudXb1K

0

Good luck with having someone actually go through the trouble to debug your script. First of all, you should provide us with tree hierarchy that matters for the script, so we can debug it easier. Second, you should put in print statements to certain parts of code and see what gets printed.

0

Yeah I used print statements and was able to figure it out. Thanks!