(HELP) How to make a team that cannot kill, or be killed by anyone else?

Hello! This may seem like a very simple task, but I am currently trying to get into the swing of learning how to script/code things. One thing I would like to get down is something that would prevent another team from killing another team. For example, in my game i’m working on I want to make it so people on the “Police” team cannot kill Civilians, and also the “Mafia” team cannot kill people on the “Civilians” team. Along with that, I would also like to make it so Civilians cannot kill anyone in the game, so its mainly just Police V. Mafia combat, and basically the Civilian team is the neutral team that’s just kind of doing their own thing.

2 Likes

You should probably rewrite the title of this post. It doesn’t really entail all you are requesting to know.

1 Like

Personally I would insert a script into StarterPlayer>StarterCharacterScripts
from there I would get the player from their character and check which team they are on.

Once you know what team they are on you can insert a ForceField into their character, which will stop them from taking any damage.

The Developer Hub is a good resource for starting out and for when you need to look up information.

Player

As for making sure they can’t kill you’d have to implement checks somewhere else on the server side following the same or similar steps.

This is all logic that your server will end up facilitating since you will, at some point, require a remote so that the client can tell the server that it used a tool and needs to hurt someone. The solution can be as simple or as complicated as you want.

In your case, since your options are very binary and only between three teams, you’ll probably have an easy time with designating who can attack who. The server’s work will come down to using teams as a conditional check. You can do this before security checks in the remote so your server doesn’t do unnecessary work when the player isn’t allowed to attack in a certain case in the first place.

The server’s checking work will come down to checking if a player is on an opposite team (if Police, victim must be Mafia and vice versa), while any damage requests from players on the Civilian team are rejected immediately. Example code:

local function canHurtVictim(player, victim)
    local playerTeam = player.Team.Name
    local victimTeam = victim.Team.Name

    if (playerTeam == "Police" and victimTeam == "Mafia") or (playerTeam == "Mafia" and victimTeam == "Police") or not (playerTeam == "Civilian") then
        -- Yes they can! Do something.
    end
end

A note on the above code sample: you can even get rid of the Civilian check (starting from or not to the last bracket). If the player is on the Civilian team, they’ll fail the checks implicitly because their team won’t match Police or Mafia, a requirement demanded by the first operand.

8 Likes