#My capture the flag system is not working properly

5 messages · Page 1 of 1 (latest)

ancient bear
#

local workspaceFlagsFolder = workspace.Flags

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TweenService = game:GetService('TweenService')
local flagContainer = workspace['Flagggg']

-- Table to keep track of point-adding coroutines for each flag
local pointCoroutines = {}
-- Track current invading team for each flag
local currentInvadingTeams = {}

local function resetGame()
-- Reset points for all teams
for _, teamPoints in pairs(ReplicatedStorage.Leaderstats_POINTS:GetChildren()) do
teamPoints.Value = 0
end

-- Destroy all flags in the flag container
for _, flag in pairs(flagContainer:GetChildren()) do
    if flag then
        flag:Destroy()
    end
end

-- Cancel all active coroutines and clear the table
for flagName, coroutine in pairs(pointCoroutines) do
    if coroutine then
        task.cancel(coroutine)
        pointCoroutines[flagName] = nil
    end
end

-- Reset invading teams table
for flagName in pairs(currentInvadingTeams) do
    currentInvadingTeams[flagName] = nil
end

end

local function startPointAddingCoroutine(flagName, teamName)
if pointCoroutines[flagName] then
task.cancel(pointCoroutines[flagName])
end

pointCoroutines[flagName] = task.spawn(function()
    while currentInvadingTeams[flagName] do
        local teamPoints = ReplicatedStorage.Leaderstats_POINTS[teamName]
        teamPoints.Value += 1

        if teamPoints.Value >= 5 then
            resetGame()
            break
        end
        task.wait(5)
    end
end)

end

#

for _, child in workspaceFlagsFolder:GetChildren() do
local pv1 = child.poss[1].Position
local pv2 = child.poss[2].Position

child.Block.Proxi.Triggered:Connect(function(playerWhoTriggered)
    local playerTeamName = playerWhoTriggered.Team.Name
    local flagName = child.Name

    if playerTeamName ~= currentInvadingTeams[flagName] then
        currentInvadingTeams[flagName] = playerTeamName

        -- Destroy any existing flag template
        if flagContainer:FindFirstChild(flagName) then
            flagContainer[flagName]:Destroy()
        end

        -- Create new flag template
        local template = ReplicatedStorage.Flags[playerTeamName]:Clone()
        template.Name = flagName  -- Name the template so it can be referenced for destruction
        local v1 = template.CFrame - template.CFrame.Position
        template.CFrame = CFrame.new(pv1) * v1
        template.Parent = flagContainer

        -- Create animation tween
        local tween = TweenService:Create(template, TweenInfo.new(3), {CFrame = CFrame.new(pv2) * v1})
        tween:Play()

        tween.Completed:Connect(function()
            -- Start adding points after the animation completes
            startPointAddingCoroutine(flagName, playerTeamName)
        end)
    end
end)

end

-- Ensure the game can be reset and flags recaptured
resetGame() `

gilded talon
#

seems chatgpt

ancient bear
#

yeah it is chat gpt but I just asked chat gpt first to remake my script but it is still not working