I need help resetting Flags after Round End

With this script when a team captures all 5 flags the enemy team dies which my other script declaires the team with remaining players the winners and fires RoundEnd RemoteEvent, the problem is the flags don’t reset when RoundEnd is fired. This is located within ServerScriptService.

-- Configuration
local flagCaptures = workspace:WaitForChild("FlagCaptures"):GetChildren() -- Folder containing all "Flag Capture" models
local lobbyTeamName = "Lobby" -- The team to exclude from dying
local debounceTime = 2 -- Time to debounce proximity prompt activation

-- Variables
local flagsOwnership = {} -- Tracks which team owns each flag
local flagCount = #flagCaptures -- Total number of flags

-- RemoteEvent for round end
local roundEndEvent = game.ReplicatedStorage:WaitForChild("RoundEnd") -- RemoteEvent for round end

-- Helper function to check if a team owns all flags
local function checkTeamOwnership()
	local teamCount = {}
	for _, owner in pairs(flagsOwnership) do
		teamCount[owner] = (teamCount[owner] or 0) + 1
	end

	for team, count in pairs(teamCount) do
		if count == flagCount then
			return team
		end
	end
	return nil
end

-- Function to handle flag capture through ProximityPrompt
local function captureFlag(player, flagCapture)
	local team = player.Team
	if not team or team.Name == lobbyTeamName then return end -- Skip if player has no team or is in the Lobby team

	local flag = flagCapture:FindFirstChild("Flag")
	if flag then
		flag.BrickColor = team.TeamColor -- Change flag color to the team's color
		flagsOwnership[flagCapture] = team.Name -- Update ownership
	end

	-- Check if a team owns all flags
	local winningTeam = checkTeamOwnership()
	if winningTeam then
		-- Handle winning and losing teams
		for _, playerInGame in pairs(game.Players:GetPlayers()) do
			if playerInGame.Team and playerInGame.Team.Name ~= lobbyTeamName and playerInGame.Team.Name ~= winningTeam then
				playerInGame.Character:BreakJoints() -- Kill the player by breaking their character's joints
			end
		end
	end
end

-- Function to reset all flags to default (black color) at the end of the round
local function resetFlags()
	for _, flagCapture in pairs(flagCaptures) do
		print("Resetting flag for:", flagCapture.Name)  -- Debugging line

		local flag = flagCapture:FindFirstChild("Flag")
		if flag then
			flag.BrickColor = BrickColor.Black() -- Reset flag color to black
			print("Flag reset to black.")  -- Debugging line
		else
			print("Flag part not found in:", flagCapture.Name)  -- Debugging line
		end

		-- **Clearing the ownership properly**
		-- Make sure that flagCapture is being used correctly as a key
		flagsOwnership[flagCapture] = nil
		print("Ownership reset for:", flagCapture.Name)  -- Debugging line
	end
end

-- Listen for the "RoundEnd" event to reset flags
roundEndEvent.OnServerEvent:Connect(function()
	print("Round End detected, resetting flags...")  -- Debugging line
	resetFlags() -- Reset flags and set color to black
end)

-- Set up proximity prompts for each "Flag Capture" model
for _, flagCapture in pairs(flagCaptures) do
	if flagCapture:FindFirstChild("CaptureZone") then
		local captureZone = flagCapture.CaptureZone
		local proximityPrompt = captureZone:FindFirstChildOfClass("ProximityPrompt")

		-- If no ProximityPrompt, create one
		if not proximityPrompt then
			proximityPrompt = Instance.new("ProximityPrompt")
			proximityPrompt.Parent = captureZone
			proximityPrompt.ActionText = "Capture Flag"
			proximityPrompt.ObjectText = flagCapture.Name
			proximityPrompt.RequiresLineOfSight = false
			proximityPrompt.HoldDuration = 0 -- Instant capture on press
			proximityPrompt.MaxActivationDistance = 10 -- Can be adjusted
		end

		-- Function to handle the ProximityPrompt activation
		proximityPrompt.Triggered:Connect(function(player)
			captureFlag(player, flagCapture)
		end)
	end
end

1 Like

and where does it get on the resetFlags debug prints?
also what is firing the server roundEndEvent what script?

I am thinking your other script is also a server script which means you probably need to be using a bindable event or just change it to an attributechangedsignal and set an attribute on the workspace to RoundEnded then it can fire from any server script you write that in without events you don’t need

2 Likes

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