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

How to check if there are no other team members in range?

So I'm currently editing a flag capture system and I want to award a player with points while he is capturing the flag, but when an opposing team comes in range the point awarding should stop - currently it doesn't, it awards both players with points when they're in range of the flag.

So how would I check for opposing team member approaching the flag and then stop the point awarding?

I currently use this:

players = {}

for i,v in pairs(game.Players:GetPlayers()) do
table.insert(players, v)
end

game.Players.PlayerAdded:connect(function(p) table.insert(players, p) end)
bin = script.Parent.Base
neutral = script.Parent.NeutralColor.Value
distance = script.Parent.CaptureDistance.Value


while true do
	wait()
	for i,v in pairs(players) do
		if v.Character ~= nil then
			torso = v.Character:findFirstChild("Torso")
			if torso ~= nil then
				if v.Character.Humanoid.Health > 0 then
					if (torso.Position-bin.Position).magnitude <= distance then
						if v.TeamColor ~= neutral then
							if v.TeamColor ~= script.Parent.CurrentOwner.Value then
								wait(2)
								if (torso.Position-bin.Position).magnitude <= distance then
									v.leaderstats.Coins.Value = v.leaderstats.Coins.Value + 5
								end
							end
						end
					end
				end
			end
		end
	end
end

3 comments

0

you can just do local players = game.Players:GetPlayers(), although I recommend updating the table by removing players that left with the PlayerRemoving event

0

also, why use if (torso.Position-bin.Position).magnitude <= distance twice?

0

Just so the player isn't awarded the points if he leaves the area within the 2 second wait()

1 answer

You could perhaps store v in another array if the last condition passes, then iterate through said array after the for loop completes. Use a variable to determine whether everyone is on the opposing team or not.

while true do
	wait()
	local inRange = {} --New array
	for i,v in pairs(players) do
		local char = v.Character
		if char then
			local torso = v.Character:FindFirstChild("Torso")
			if torso then
				if char.Humanoid.Health > 0 and (torso.Position-bin.Position).magnitude <= distance then
					inRange[#inRange+1] = v; --Store in array
				end
			end
		end
	end
	--Now you know if there are opposing members in range like so
	local onlyOpposing = true;
	for _,v in pairs(inRange) do
		if v.TeamColor == script.Parent.CurrentOwner.Value then
			onlyOpposing = false;
			break;
		end
	end
	if onlyOpposing then
		--You may award them
	end
end

######Note: You should remove indexes from the players array using the PlayerRemoving event, to sync it to the server correctly.

1 comment

0

Hey I don't really get how this is meant to work, I added the awarding where you specified, but still nothing and no errors.

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