How can I insert an Anti-Teamkill script into my gun?

Hello, I am working on a game and I don’t have much experience. There are two different teams, but people can teamkill in my game, can someone make a script for the blue team gun? Maybe

if target player is red team then do damage else do nothing

or something like that. Here’s the script:

local debounce = false
local fireRate = 1 

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	if debounce == false then
		debounce = true
		target.Humanoid:TakeDamage(30)
		task.wait(fireRate)
		debounce = false
	end
end)
1 Like

And yes I know to swap the red team part for blue team for the red team weapons

1 Like

that kind of script is that that doesn’t do any raycasting validation on server?

I dont know what raycasting is so that should tell you what level of experience i have :)))) i dont know how to make stuff :sob: :sob: :sob:

1 Like

Raycasting just shoots a laser from point A to point B and gives you info on what it collided with.

oh cool thanks for that information but unfortunately i dont know how to use it

this should help. your script should follow something like. “when shoot, fire raycast, if raycast hit is player and hit.players_team is {opponent} then dealDamage()” the variables are examples only

in that document theres some example of use cases

I’d recommend watching a tutorial about them on youtube. For the actual problem itself you want to be checking the player’s Team or TeamColor property to check if they are on the same team.

if plr1.TeamColor == plr2.TeamColor then 
    -- script
end

Ok, thank you but I don’t really want to change up my script, since it works fine right now, except for the teamkilling part.

Also you should definitely do some basic validation as exploiters (even though it’s rare to see them) can just loop thru each player in the game and fire the remote event which can allow them to loop kill the entire server.

Does “==” mean equal to or not equal to?

Player 1 team == player 2 team then do damage else end

i dont understand how your actual system works currently. if you dont want to change much of the current system, could you send some snippets of your code?

Well… that is the entire code… It’s very very VERY simple but its a gun system

Go on youtube and look for scripting tutorial playlists (i recommend devking), learning the basics is essential if you want your game to get anywhere.
(== means equal and ~= means not equal)

Ok thank you, i’ll try something real quick

I tried this with ~= (not equal) and it did not damage anyone. Or am I supposed to write

Player 1 team == player 2 team then end else do damage

Target is a character, not a player, so you’ll need to use plr2 = game.Players:GetPlayerFromCharacter(target) to define the player.

You should be using not equal, that’s correct.

Also reiterating everyone else’s concerns that this script is fine for learning or for a personal project, but if you use this in a game that ever gets a reasonable amount of players, exploiters will use this code to loop kill everyone in the server. This isn’t suitable for use in an actual public game.

One issue I do see here is that debounce is global, meaning only one player can deal damage every second; if two people shoot each other at the same time, only one of them will deal damage. You should find another way to track your debounce.

You’d want to:

  • Get the target player. This is done using Players:GetPlayerFromCharacter(target).
  • If same team as the person who fired, do no damage.
local Players = game:GetService("Players") -- get the Players service

local debounce = false
local fireRate = 1 

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
	-- get the target player
	local targetPlayer = Players:GetPlayerFromCharacter(target)
	
	-- check:
		-- if target player exists,
		-- and target player's team is equal to the player's team
	if targetPlayer and targetPlayer.TeamColor == player.TeamColor then
		return -- do nothing
	end
	
	if debounce == false then
		debounce = true
		target.Humanoid:TakeDamage(30)
		task.wait(fireRate)
		debounce = false
	end
end)
  1. How can exploiters loop kill everyone? I really don’t know.
  2. How can I fix the debounce issue? Should I make it a local script instead?

Also the Roblox Assistant gave me a script for the Anti-Teamkill.

Edit: I tested the debounce thing with an alt and we did damage at the same time, I don’t know why you said there was an issue?

1 Like