I, nor should anyone else, will not write you out a script as it would be fairly fat in size and needs to be written to work with your other scripts. However, I can lead you in the directions to do something like this.
Objective
Prevent npc's, specifically kings, from taking damage from the same team.
Direction
Assuming you haven't written any scripts for damaging an NPC. Assign your players and NPC's into their respective teams. Setup the weaponry so that they can detect collisions between the weapon part and other parts.
When the weapon detects collisions, determine if the collided part is a player. Feel free to write anti-exploit parts here, such as magnitude checks or cool-downs between use of weapon.
Afterwards, invoke a remote so the server can receive information about what user was hit and who hit the user. Since a local script will be invoking the remote, the first parameter will already be the local player, so you can just pass the other player.
In a server script, bind a function to the remote. When the remote is invoked, compare the attacker (first parameter) and the attacked (second parameter) to see if they are in the same team. Either use the team service or some other form of system to manage that.
If they are in opposing teams, check their distance from each other and determine whether it was possible for a collision (Depends on if you incorporate guns into your game). Finally, deal damage to the health.
tl;dr
Remotes, functions, touched event, and if statements.
Other Notes
I don't believe using the Team Service will be very effective in this scenario, since you are also dealing with NPC's. I'd recommend you look up videos on how to create custom teams. One way I do it is forming a leaderstat-like system, but that requires quite a few things:
- Make sure the teams are balanced
- Have the teams auto-balance
- Make sure a player is assigned a team upon entering the game
- Make sure the NPC's are assigned a team upon spawning in (or have it pre-assigned and just replicate its model into the workspace)
Overall
I apologize for this lengthy explanation, but this should give you a general idea as to how to develop a basic combat system that prevents team killing.
3 comments
check if a hit on the npc came from someone on another team, then deal damage
Yeah simply, do that
this is not a request site kiddo, also learn grammar you trash requester