When a player fires a gun, it sends who is supposed to be shot to the server. The server is supposed to check if they’re a friendly, and damage them accordingly. This doesn’t work however, and it picks everyone up as a friendly. Anyone able to help?
Variables:
Target = Player who’s supposed to be shot
player = Player firing the gun
Damage = How much damage they take from the shot
if game.Players:GetPlayerFromCharacter(Target).Team == game.Teams["Ethics Committee"] or game.Teams["Mobile Task Force"] or game.Teams["O5 Council"] or game.Teams["Science Department"] or game.Teams["Security Department"] and player.Team == game.Teams["Ethics Committee"] or game.Teams["Mobile Task Force"] or game.Teams["O5 Council"] or game.Teams["Science Department"] or game.Teams["Security Department"] then
print("No teamkilling!")
elseif game.Players:GetPlayerFromCharacter(Target).Team == game.Teams["Chaos Insurgency"] or game.Teams["Class-D"] and player.Team == game.Teams["Chaos Insurgency"] or game.Teams["Class-D"] then
print("No teamkilling!")
else
Target.Humanoid:TakeDamage(Damage)
end
I also find that this method is highly inefficient.
Imagine that you have 50 teams, do you want to write this 50 times? Use a table system, because with this you must write game.Players:GetPlayerFromCharacter(Target).Team == game.Teams["Ethics Committee"] every time for every team.
You’re using ors incorrectly, the reason is because you’re first checkign a condition then just checkign something by itself which if it is not nil, always returns true, so thus it will always assume everyone is friendly. I would honestly recommend a table of al lthe team names to not injure and to improve organization
local safeTeams = {
"Ethics Commitee",
"Mobile Task Force",
--Continue
}
local playertarget = game.Players:GetPlayerFromCharacter(Target)
if table.find(safeTeams,playertarget.Team.Name) then
print("No teamkilling!")
else
Target.Humanoid:TakeDamage(Damage)
end
The way you were setting it up wouldn’t work. Let’s take an example, say y ou have 3 variables, a, b, and c
local a = 10
local b = 5
local c = 10
And you want to do something if a is equal to one of them, so you do
if a == b or c then
And it works, but what if you change c to 9, it’ll still work because c has a value, it is not nil nor false and you are not comparing it with anything, you had to do
if a == b or a == c then
That’s why your code always assumed everyone was friendly
That’s your issue as you how you were doing it, are you trying to make it so if they’re both on the same team, don’t damage?
You could do tables to store which teams are friendly to who, I think something like this would work
local safeTeamsTbl = {
{
"Ethics Commitee",
"O5",
--Continue if needed
},
{
"Mobile Task Force",
"Security",
--Continue if needed
}
--Continue with team friendly tables
}
local target = game.Players:GetPlayerFromCharacter(Target)
local friendly = false
for _,tbl in pairs(safeTeamsTbl) do
if table.find(tbl, target.Team.Name) and table.find(tbl, player.Team.Name) then
friendly = true
print("No teamkilling!")
break
end
end
if not friendly then
Target.Humanoid:TakeDamage(Damage)
end