Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I find the players team to make anti tk?

I tried multiple times but failed

This is the function that detects for a hit (also this gun uses ray casting).

The main point of this question is to find out how to make anti team kill in this function.

local function hit(plr, part)
	if not canShoot() then return end
	if not part then return end
	
	local character = player.Character or player.CharacterAdded:Wait()
	local rootPart = character:WaitForChild("HumanoidRootPart")
	
	if rootPart then
		if (rootPart.CFrame.p - part.CFrame.p).magnitude <= range.Value then
			local humanoid
			
			if part.Name == "Handle" then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid") 	
			else
				humanoid = part.Parent:FindFirstChild("Humanoid")
			end
			
			
			if humanoid and humanoid.Health > 0 then
				local newDamage = damage.Value
				
				if part.Name == "Head" then
					newDamage = newDamage * headMultiplier.Value
					
					local soundCopy = headshotSound:Clone()
					soundCopy.Parent = part
					soundCopy:Play()
				end
				
				humanoid:TakeDamage(newDamage)
			end
		end
	end
end

2 answers

Given the character you can use:

local player = game.Players:GetPlayerFromCharacter(character)

then you can just use the player's team color to detect if they are on the team that you're checking for.

local player = game.Players:GetPlayerFromCharacter(character)
if player.TeamColor == whateverTeam'sTeamColor then
--don't do damage
else
--do damage!
end

18 comments

0

wait where does this go again?

0

I’d put it after the hit is detected and after you got the character

0

Don’t forget to fill in the damage in the if statement. Alternatively you can just return if they are on the same team with no damage being done since you exited

0
0

Seems resonable

0

Doesn't work. : /

0

Any errors? Any leads? From what I see why did you choose the team color == 8?

0

nope, no errors at all, also i have no idea but i did chose a team color, the 8 isnt there anymore

0

From what I can see, your if statements are a bit messed up. that "return end" should just have return and then move on to the else statement. Right now the else is saying "If there isn't a humanoid then try to do damage."

0

Changed return end to return but it still doesn't work (it doesnt do damage)

0

If you want send an updated screenshot, also if any new errors have occurred that may be helpful as well. Sorry this is taking so long!

0

It might be a good time to try some debugging methods, such as print debugging. We need to find out where exactly this code is stopping. So sprinkle some prints around and lets figure out where it stops. It should be noted that this could potentially be a problem if you have this in a local script and you are running it with filtering enabled, you'll need to set up an event to do damage on the

0

server

0

This is in a server script btw.

0

I put some prints in the if and else and it does not print anything when hitting teammates and enemies

0

That likely means it's not even getting to that point! You should try putting prints higher up in the function, like before CanShoot, etc.

0

I can probably team create you then, cause this is getting confusing atm. Or I can use codeshare.

The Players service has a function, which is GetPlayerFromCharacter.

It expects an instance as an argument, and if it is a player's character, the player is returned and now you have the player. It will return nil if the instance is not a character.

local targetplayer = game.Players:GetPlayerFromCharacter(part.Parent)

if not targetplayer then
    -- Maybe hit an accessory
    targetplayer = game.Players:GetPlayerFromCharacter(part.Parent.Parent)
end

if targetplayer and targetplayer.TeamColor ~= player.TeamColor then
    -- Do stuff
end

This discussion is preserved as it was. New answers, comments, and votes are closed.