#what is the issue?

1 messages · Page 1 of 1 (latest)

drowsy pecan
#

this code seems perfectly fine, yet when i touch the blue flag, i get like 500 captures spammed without returning to the red flag

local flagB = game.Workspace.flags.flagBlue
local hitboxR = flagR:FindFirstChild("flagHitbox")
local hitboxB = flagB:FindFirstChild("flagHitbox")
local flagHolderR = nil
local flagHolderB = nil
local flagStandPosR = game.Workspace.redSide.flagStandRed.Position
local flagStandPosB = game.Workspace.blueSide.flagStandBlue.Position
local flagRPos = flagR.PrimaryPart.Position
local flagBPos = flagB.PrimaryPart.Position
local redCaptures = 0
local blueCaptures = 0


hitboxB.Touched:connect(function(hit) -- BLUE
   local h = hit.Parent:FindFirstChild("Humanoid")
   if not h then return end
   local c = h.Parent
   local p = game.Players:GetPlayerFromCharacter(c)
   if not p then return end
   if p.Team == game.Teams.red then
       print(p.Team)
       flagHolderB = p
       print(flagHolderR)
       print(flagHolderB)
       print(p)
   end
   if flagHolderR == nil then return end
   if p.Team == game.Teams.blue and flagHolderR == p then
       flagHolderR = nil
       blueCaptures = blueCaptures + 1
       returnFlag(flagR)
       print("blue capture", blueCaptures)
   end
end)

hitboxR.Touched:connect(function(hit) -- RED
   local h = hit.Parent:FindFirstChild("Humanoid")
   if not h then return end

   local c = h.Parent
   local p = game.Players:GetPlayerFromCharacter(c)
   if not p then return end

   if p.Team == game.Teams.blue then
       flagHolderR = p
   end
   if flagHolderB == nil then return end
   if p.Team == game.Teams.red and flagHolderB == p then
       flagHolderB = nil
       redCaptures = redCaptures + 1
       returnFlag(flagB)
       print("red capture", redCaptures)
   end
end)


function returnFlag(flag)
   if flag == flagR then
       flagRPos = flagStandPosR
   else
       flagBPos = flagStandPosB
   end
end```
#

all oft he variables are fine

#

the code is meant to be capture the flag

heady cosmos
#
local flagR = game.Workspace.flags.flagRed
local flagB = game.Workspace.flags.flagBlue
local hitboxR = flagR:FindFirstChild("flagHitbox")
local hitboxB = flagB:FindFirstChild("flagHitbox")
local flagHolderR = nil
local flagHolderB = nil
local flagStandPosR = game.Workspace.redSide.flagStandRed.Position
local flagStandPosB = game.Workspace.blueSide.flagStandBlue.Position
local flagRPos = flagR.PrimaryPart.Position
local flagBPos = flagB.PrimaryPart.Position
local redCaptures = 0
local blueCaptures = 0


hitboxB.Touched:connect(function(hit) -- BLUE
    local h = hit.Parent:FindFirstChild("Humanoid")
    if not h then return end
    local c = h.Parent
    local p = game.Players:GetPlayerFromCharacter(c)
    if not p then return end
    if p.Team == game.Teams.red then
        print(p.Team)
        flagHolderB = p
        print(flagHolderR)
        print(flagHolderB)
        print(p)
    end
    if flagHolderR == nil then return end
    if p.Team == game.Teams.blue and flagHolderR == p then
        flagHolderR = nil
        blueCaptures = blueCaptures + 1
        returnFlag(flagR)
        print("blue capture", blueCaptures)
    end
end)

hitboxR.Touched:connect(function(hit) -- RED
    local h = hit.Parent:FindFirstChild("Humanoid")
    if not h then return end

    local c = h.Parent
    local p = game.Players:GetPlayerFromCharacter(c)
    if not p then return end

    if p.Team == game.Teams.blue then
        flagHolderR = p
    end
    if flagHolderB == nil then return end
    if p.Team == game.Teams.red and flagHolderB == p then
        flagHolderB = nil
        redCaptures = redCaptures + 1
        returnFlag(flagB)
        print("red capture", redCaptures)
    end
end)


function returnFlag(flag)
    if flag == flagR then
        flagRPos = flagStandPosR
    else
        flagBPos = flagStandPosB
    end
end
drowsy pecan
#

that did the same thing

#

as my old code

heady cosmos
#

its the same thing

#

just easier to read

drowsy pecan
simple houndBOT
#

studio** You are now Level 3! **studio

drowsy pecan
#

but that wasn’t really the issue

#

the issue is that it don’t work

deft hamlet
# drowsy pecan this code seems perfectly fine, yet when i touch the blue flag, i get like 500 c...

the issue is that Touched events fire continuously while a player remains in contact with the part ...
when you touch the blue flag the event keeps firing repeatedly causing the spam.

you need to add debounce/cooldown logic to prevent multiple triggers

local flagR = game.Workspace.flags.flagRed
local flagB = game.Workspace.flags.flagBlue
local hitboxR = flagR:FindFirstChild("flagHitbox")
local hitboxB = flagB:FindFirstChild("flagHitbox")
local flagHolderR = nil
local flagHolderB = nil
local flagStandPosR = game.Workspace.redSide.flagStandRed.Position
local flagStandPosB = game.Workspace.blueSide.flagStandBlue.Position
local flagRPos = flagR.PrimaryPart.Position
local flagBPos = flagB.PrimaryPart.Position
local redCaptures = 0
local blueCaptures = 0

local lastTouchB = {}
local lastTouchR = {}
local COOLDOWN = 1

hitboxB.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if not h then return end
    local c = h.Parent
    local p = game.Players:GetPlayerFromCharacter(c)
    if not p then return end
    
    if lastTouchB[p] and tick() - lastTouchB[p] < COOLDOWN then
        return
    end
    lastTouchB[p] = tick()
    
    if p.Team == game.Teams.red then
        flagHolderB = p
    end
    if flagHolderR == nil then return end
    if p.Team == game.Teams.blue and flagHolderR == p then
        flagHolderR = nil
        blueCaptures = blueCaptures + 1
        returnFlag(flagR)
        print("blue capture", blueCaptures)
    end
end)

hitboxR.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if not h then return end
    local c = h.Parent
    local p = game.Players:GetPlayerFromCharacter(c)
    if not p then return end
    
    if lastTouchR[p] and tick() - lastTouchR[p] < COOLDOWN then
        return
    end
    lastTouchR[p] = tick()
    
    if p.Team == game.Teams.blue then
        flagHolderR = p
    end
    if flagHolderB == nil then return end
    if p.Team == game.Teams.red and flagHolderB == p then
        flagHolderB = nil
        redCaptures = redCaptures + 1
        returnFlag(flagB)
        print("red capture", redCaptures)
    end
end)

function returnFlag(flag)
    if flag == flagR then
        flagRPos = flagStandPosR
    else
        flagBPos = flagStandPosB
    end
end

with this , i added a 1 second cooldown per player , to prevent the spam

drowsy pecan
#

ur too late bucko

#

i fixed it

drowsy pecan
#

appreciate it

deft hamlet
#

well

#

teach me

#

how did you fix it

drowsy pecan
#

and it fixed itself

#

idk why

#

i am adding carrying the flag now