Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I detect flag captures?

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)

3 comments

0

What makes the flag do anything?

0

when a person on a team caps it adds 1 to their team score

0

i dont really know what you mean

1 answer

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)

4 comments

0

"stands" cant be defined

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.

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?

0

i have my leaderboard script edited into the post

This discussion is preserved as it was. New answers, comments, and votes are closed.