Flag only works twice before breaking

I’d hate to make another post on here within the same day, but I have no choice. This is a king of the hill, team vs team thing that I’m making. The issue I have is that after the second time of the flag going down and up it stops working (I made sure it wasn’t the Vietcong flag that was bugged, the USA flag also had the problem too). It just stops responding, you can look at the prints. (Tween 1 = Down, Tween 2 = Up). I have a theory for my issue, maybe the player is sending to many things to the server script (idk just a guess).

Screenshot_20230630_105527

local Part = script.Parent
local Teams = game:GetService("Teams")
local Blue = Teams["U.S Marines"]
local Red = Teams.Vietcong
local Flag = workspace["King of Hill 1"].Flag.Flag
local TweenService = game:GetService("TweenService")
local FlagPrimary = Flag.Top

local playersTouching = {} -- Store the players currently touching the part
local isFlagMoving = false -- Flag state variable to track if the flag is currently moving
local isBlueFlagUp = false -- Flag state variable to track if the blue flag is up
local isRedFlagUp = false -- Flag state variable to track if the red flag is up

local function UpdateColor()
	local BlueCount = 0
	local RedCount = 0

	for _, player in ipairs(playersTouching) do
		if player.Team == Blue then
			BlueCount = BlueCount + 1
		elseif player.Team == Red then
			RedCount = RedCount + 1
		end
	end

	if BlueCount > RedCount and not isBlueFlagUp then
		if not isFlagMoving then -- Check if the flag is not currently moving
			isFlagMoving = true -- Set the flag state to moving
			isBlueFlagUp = true -- Set the blue flag state to up
			for i = 1, 32 do
				-- Tween stuff
				local Info = TweenInfo.new(7, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
				local Goal1 = {CFrame = FlagPrimary.CFrame - Vector3.new(0, 26.5, 0)}
				local Goal2 = {CFrame = FlagPrimary.CFrame}
				local Tween1 = TweenService:Create(FlagPrimary, Info, Goal1)
				local Tween2 = TweenService:Create(FlagPrimary, Info, Goal2)

				Tween1.Completed:Connect(function()
					wait(8)
					Flag[tostring(i)].TextureID = "rbxassetid://114480789"
					Flag["32"].TextureID = "rbxassetid://114480789"
					wait(2)
					Tween2:Play()
					print("Tween2 Done")
				end)

				Tween1:Play()
				print("Tween1 Done")
			end
			wait(17) -- Wait for the flag tweens to complete
			isFlagMoving = false -- Set the flag state to not moving
		end
	elseif RedCount > BlueCount and not isRedFlagUp then
		if not isFlagMoving then -- Check if the flag is not currently moving
			isFlagMoving = true -- Set the flag state to moving
			isRedFlagUp = true -- Set the red flag state to up
			for i = 1, 32 do
				-- Tween stuff
				local Info = TweenInfo.new(7, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
				local Goal1 = {CFrame = FlagPrimary.CFrame - Vector3.new(0, 26.5, 0)}
				local Goal2 = {CFrame = FlagPrimary.CFrame}
				local Tween1 = TweenService:Create(FlagPrimary, Info, Goal1)
				local Tween2 = TweenService:Create(FlagPrimary, Info, Goal2)

				Tween1.Completed:Connect(function()
					wait(8)
					Flag[tostring(i)].TextureID = "rbxassetid://144960721"
					Flag["32"].TextureID = "rbxassetid://144960721"
					wait(2)
					Tween2:Play()
					print("Tween2 Done")

				end)

				Tween1:Play()
				print("Tween1 Done")

			end
			wait(17) -- Wait for the flag tweens to complete
			isFlagMoving = false -- Set the flag state to not moving
		end
	end
end
-- When a player touches the part, add them to the playersTouching table
local function onPartTouched(otherPart)
	local character = otherPart.Parent
	if character and character:IsA("Model") then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			local player = game.Players:GetPlayerFromCharacter(character)
			if player then
				table.insert(playersTouching, player)
				UpdateColor() -- Update the color when a player touches the part
			end
		end
	end
end

-- When a player stops touching the part, remove them from the playersTouching table
local function onPartTouchEnded(otherPart)
	local character = otherPart.Parent
	if character and character:IsA("Model") then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			local player = game.Players:GetPlayerFromCharacter(character)
			if player then
				for i, touchedPlayer in ipairs(playersTouching) do
					if touchedPlayer == player then
						table.remove(playersTouching, i)
						UpdateColor() -- Update the color when a player stops touching the part
						break
					end
				end
			end
		end
	end
end

Part.Touched:Connect(onPartTouched)
Part.TouchEnded:Connect(onPartTouchEnded)
2 Likes

your video doesnt work please upload to streamable or just give us the file.

Mb, the video was too short for imgur, so I tried Discord. Here’s a YouTube link for the video.

I think this solved it:

local function UpdateColor()
	local BlueCount = 0
	local RedCount = 0

	for _, player in ipairs(playersTouching) do
		if player.Team == Blue then
			BlueCount = BlueCount + 1
		elseif player.Team == Red then
			RedCount = RedCount + 1
		end
	end

	if BlueCount > RedCount and not isBlueFlagUp then
		if not isFlagMoving then -- Check if the flag is not currently moving
			isFlagMoving = true -- Set the flag state to moving
			isBlueFlagUp = true -- Set the blue flag state to up
			isRedFlagUp = false
			for i = 1, 32 do
				-- Tween stuff
				local Info = TweenInfo.new(7, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
				local Goal1 = {CFrame = FlagPrimary.CFrame - Vector3.new(0, 26.5, 0)}
				local Goal2 = {CFrame = FlagPrimary.CFrame}
				local Tween1 = TweenService:Create(FlagPrimary, Info, Goal1)
				local Tween2 = TweenService:Create(FlagPrimary, Info, Goal2)

				Tween1.Completed:Connect(function()
					wait(8)
					Flag[tostring(i)].TextureID = "rbxassetid://114480789"
					Flag["32"].TextureID = "rbxassetid://114480789"
					wait(2)
					Tween2:Play()
					print("Tween2 Done")
				end)

				Tween1:Play()
				print("Tween1 Done")
			end
			wait(17) -- Wait for the flag tweens to complete
			isFlagMoving = false -- Set the flag state to not moving
		end
	elseif RedCount > BlueCount and not isRedFlagUp then
		if not isFlagMoving then -- Check if the flag is not currently moving
			isFlagMoving = true -- Set the flag state to moving
			isRedFlagUp = true -- Set the red flag state to up
			isBlueFlagUp = false
			for i = 1, 32 do
				-- Tween stuff
				local Info = TweenInfo.new(7, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
				local Goal1 = {CFrame = FlagPrimary.CFrame - Vector3.new(0, 26.5, 0)}
				local Goal2 = {CFrame = FlagPrimary.CFrame}
				local Tween1 = TweenService:Create(FlagPrimary, Info, Goal1)
				local Tween2 = TweenService:Create(FlagPrimary, Info, Goal2)

				Tween1.Completed:Connect(function()
					wait(8)
					Flag[tostring(i)].TextureID = "rbxassetid://144960721"
					Flag["32"].TextureID = "rbxassetid://144960721"
					wait(2)
					Tween2:Play()
					print("Tween2 Done")

				end)

				Tween1:Play()
				print("Tween1 Done")

			end
			wait(17) -- Wait for the flag tweens to complete
			isFlagMoving = false -- Set the flag state to not moving
		end
	end
end

because you were not setting the isblueflagup and isredflagup variables back to false that caused the if statement to not work.

1 Like

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