Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I detect flag captures?

Asked by 9 years ago

Take this as a question and not a request.

How would I detect when someone captures a flag on a team because I'm making a CTF gamemode. What events would I use to achieve this.

My leaderboard script

local stats = {"value", "Omegite", "Captures"}

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Model", player)
    leaderstats.Name = "leaderstats"
    for _, stat in pairs(stats) do
        Instance.new("IntValue", leaderstats).Name = stat
    end
end)
0
What makes the flag do anything? BlueTaslem 18071 — 9y
0
when a person on a team caps it adds 1 to their team score rabidhalofan12345 55 — 9y
0
i dont really know what you mean rabidhalofan12345 55 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

Try this script:

function findAllFlagStands(root)
    local c = root:children()
    for i=1,#c do
        if (c[i].className == "Model" or c[i].className == "Part") then
            findAllFlagStands(c[i])
        end
        if (c[i].className == "FlagStand") then
            table.insert(stands, c[i])
        end
    end
end

function hookUpListeners()
    for i=1,#stands do
        stands[i].FlagCaptured:connect(onCaptureScored)
    end
end

function onPlayerEntered(newPlayer)


    local stats = Instance.new("IntValue")
    stats.Name = "leaderstats"

    local captures = Instance.new("IntValue")
    captures.Name = "Captures"
    captures.Value = 0


    captures.Parent = stats


    while true do
        if newPlayer.Character ~= nil then break end
        wait(5)
    end

    stats.Parent = newPlayer

end


function onCaptureScored(player)

        local ls = player:findFirstChild("leaderstats")
        if ls == nil then return end
        local caps = ls:findFirstChild("Captures")
        if caps == nil then return end
        caps.Value = caps.Value + 1

end


findAllFlagStands(game.Workspace)
hookUpListeners()
game.Players.ChildAdded:connect(onPlayerEntered)

0
"stands" cant be defined rabidhalofan12345 55 — 9y
0
Hm...What's the name of your flag? It should work if a flag has a flag property named "FlagStand" regardless of the color. CowardlyArnold 20 — 9y
0
Ok I see the probelm. The stands are being detected because i'm doing map changes so when the game starts they are in a lobby. Is there a solution to this? rabidhalofan12345 55 — 9y
0
i have my leaderboard script edited into the post rabidhalofan12345 55 — 9y
Ad

Answer this question