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
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
players
array using the PlayerRemoving
event, to sync it to the server correctly.