Capture Debounce Help

The issue I’m having is that yes, I did add a debounce every time a player captures one of the towns in the game however, the player isn’t able to capture the town again if it gets recaptured by the enemy team/country. I tried setting the debounce to false at a certain condition but still no result.

-- Server script for each town
local module = require(game.ServerScriptService.Modules.TownCaptureModule)

local townhall = script.Parent
local towncapturepart = townhall.TownCapturePart
local debounce = false

towncapturepart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("SoldierStats") then
		if debounce == false and hit.Parent.SoldierStats.NationName.Value == "Yellowstone" and townhall.CurrentCountry.Value == "Yellowstone" then

		else
			debounce = true

			module.CaptureTown(townhall, townhall.TownName.Value, hit.Parent.SoldierStats.FlagID.Value, hit.Parent.SoldierStats.NationName.Value)
			
			
			
			--[[
			if hit.Parent.SoldierStats.NationName.Value ~= townhall.CurrentCountry.Value then
				debounce = false
			end
			]]--
			--[[
			towncapturepart.TouchEnded:Connect(function()
				debounce = false
			end)
			]]--
		end	
	end
end)
-- Module script for capturing towns
local module = {}

function module.PlayCaptureSound()
	local sound = Instance.new("Sound")
	sound.Parent = workspace.Map.Sounds
	sound.Name = "CaptureSound"
	sound.Volume = 10
	sound.SoundId = "rbxassetid://233422074"
	sound:Play()
end

function module.AnnounceWinningTeam(team) --// team/country, same thing lol
	for i, player in pairs(game.Players:GetPlayers()) do
		local playergui = player:FindFirstChild("PlayerGui")

		if playergui then
			local textlabel = Instance.new("TextLabel")
			textlabel.Parent = playergui.Main
			textlabel.Name = "WinnerTeamAnnouncementText"
			textlabel.BackgroundTransparency = 1
			textlabel.BorderSizePixel = 0
			textlabel.Position = UDim2.new(.344, 0, .156, 0)
			textlabel.Size = UDim2.new(0, 600, 0, 50)
			textlabel.Font = Enum.Font.RobotoMono
			textlabel.TextScaled = true
			textlabel.TextColor3 = Color3.fromRGB(255, 255, 255)
			textlabel.TextStrokeTransparency = 0
			textlabel.TextWrapped = true
			textlabel.Text = team.." has won the war!"
			textlabel.Visible = true

			wait(3)
			textlabel:Destroy()
		end
	end
end

function module.CaptureTown(townhall, townname, flagid, currentcountry)
	townhall.CurrentCountry.Value = currentcountry

	for i, player in pairs(game.Players:GetPlayers()) do
		local playergui = player:FindFirstChild("PlayerGui")
		
		if playergui then
			local textlabel = Instance.new("TextLabel")
			textlabel.Parent = playergui.Main
			textlabel.Name = "TownCapturedText"
			textlabel.BackgroundTransparency = 1
			textlabel.BorderSizePixel = 0
			textlabel.Position = UDim2.new(.344, 0, .1, 0)
			textlabel.Size = UDim2.new(0, 600, 0, 50)
			textlabel.Font = Enum.Font.RobotoMono
			textlabel.TextScaled = true
			textlabel.TextColor3 = Color3.fromRGB(255, 255, 255)
			textlabel.TextStrokeTransparency = 0
			textlabel.TextWrapped = true
			textlabel.Text = townname.." has been captured by "..currentcountry
			textlabel.Visible = true

			module.PlayCaptureSound()

			for i, flagpart in pairs(townhall.Flag.Flag:GetChildren()) do
				flagpart.TextureID = flagid
			end
			
			--[[
			for i, town in pairs(workspace.Map:GetDescendants()) do
				if town:IsA("Model") then
					local currentcountry = town:FindFirstChild("CurrentCountry")

					if currentcountry then
						if currentcountry.Value == "Yellowstone" then
							module.AnnounceWinningTeam("Yellowstone")

						elseif currentcountry.Value == "Lakewood" then
							module.AnnounceWinningTeam("Lakewood")
							
						else

						end
					end
				end
			end
			]]--

			wait(3)
			textlabel:Destroy()
		end
	end
end

return module

It looks like debounce is never being set to true.

You are checking to see if it is false with

	if debounce == false and hit.Parent.SoldierStats.NationName.Value == "Yellowstone" and townhall.CurrentCountry.Value == "Yellowstone" then

and only set it to true if it is already true which will never happen.

Try something like this:

     if debounce == false and hit.Parent.SoldierStats.NationName.Value == "Yellowstone" and townhall.CurrentCountry.Value == "Yellowstone" then
        debounce = true
        module.CaptureTown(townhall, townhall.TownName.Value, hit.Parent.SoldierStats.FlagID.Value, hit.Parent.SoldierStats.NationName.Value)
	 else
        towncapturepart.TouchEnded:Connect(function()
		    debounce = false
		end)
     end
end)

I don’t think that will work exactly, but hopefully, you get what I mean about debounce never being set to true and can figure out how to rearrange it.

1 Like

I noticed a couple of mistakes in your snippet, but based on that snippet I managed to make some rearrangements with the code however, it still didn’t work. I’ll still look for ways to make it work. But, thanks anyway.

Alright I fixed it. Doesn’t look any similar to your solution, however, at least yours managed to inspire me to make the correction solution. Here’s the fixed code:

local module = require(game.ServerScriptService.Modules.TownCaptureModule)

local townhall = script.Parent
local towncapturepart = townhall.TownCapturePart
local debounce = false

towncapturepart.Touched:Connect(function(hit)
	--local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("SoldierStats") then
		if hit.Parent.SoldierStats.NationName.Value ~= townhall.CurrentCountry.Value then --if hit.Parent.SoldierStats.NationName.Value == "Yellowstone" and townhall.CurrentCountry.Value == "Yellowstone" then

		--else
			if debounce == false then
				debounce = true

				module.CaptureTown(townhall, townhall.TownName.Value, hit.Parent.SoldierStats.FlagID.Value, hit.Parent.SoldierStats.NationName.Value)
				
				debounce = false
				
				--[[
				if hit.Parent.SoldierStats.NationName.Value ~= townhall.CurrentCountry.Value then
					debounce = false
				end
				]]--
				
				--[[
				towncapturepart.TouchEnded:Connect(function()
					debounce = false
				end)
				]]--
			end
		end	
	end
end)