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)
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
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
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.
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?
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)
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.
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)