How would i be able to make players not able to kill players on the same team whit fastcast

well here we go, ive been trying to make a fps game and ive ran into a issue, team kill, it would be a really big issue due to greifers and stuff, ive tried writing down my own anti team kill on the onrayhit function but it doesnt seem to work, when i treid testing on a alt i could still kill players on the same team and my code dint work,

ive tried many stuff alredy like searching dev forum and switching lines of code

game.ReplicatedStorage.Events.Gunfire.OnServerEvent:Connect(function(player, MuzzlePos, mousePos, damage, limbDMG, CF, muzvel)
	
	local sound = player.Character.Torso:FindFirstChild("Fire")

	if sound then

		sound:Play()



	end
	
	castParams.FilterDescendantsInstances = {player.Character}
	castBehaviour.AutoIgnoreContainer = false
	castBehaviour.RaycastParams = castParams
	
	local origin = MuzzlePos
	local direction = (mousePos - origin)
	caster:Fire(origin, direction, muzvel, castBehaviour)
	caster.LengthChanged:Connect(onLenghtChanged)
 
local function onrayHit(cast, castResult, Velocity, flyingthing)
	
	
		if not hitDebounce[caster] then
			hitDebounce[caster] = {[flyingthing] = true}
		elseif hitDebounce[caster][flyingthing] then
			return
		else
			hitDebounce[caster][flyingthing] = true
		end
		flyingthing:Destroy()
	local hit = castResult.Instance
	local char = hit:FindFirstAncestorWhichIsA("Model")
	
	if not char:FindFirstChild("Humanoid") then
		
		local bulletHole = game.ReplicatedStorage.Effects.BulletHole:Clone()
		bulletHole.Parent = game.Workspace.Decos
		bulletHole.Name = player.Name .. "_BulletHole"
		bulletHole.CFrame = CFrame.new(castResult.Position, castResult.Position + castResult.Normal) * CFrame.new(0, 0, 0)
		game.Debris:AddItem(bulletHole, 10)
		
	end
	
	print(hit.Name)
		if hit.Name == "HumanoidRootPart" or hit.Name == "Torso" or hit.Name == "Handle" and char:FindFirstChild("Humanoid") and char.Name ~= player.Name then
			if player.Team or game:GetService("Players"):GetPlayerFromCharacter(char) == nil then
		char.Humanoid:TakeDamage(damage)
		
			elseif player.Team and game:GetService("Players"):GetPlayerFromCharacter(char) and game:GetService("Players"):GetPlayerFromCharacter(char).Team ~= player.Team then
				if char:WaitForChild("Armor") then
					if char:WaitForChild("Armor").Value == 0 then
				char.Humanoid:TakeDamage(damage)
					elseif char:WaitForChild("Armor").Value == 1 then
						local fixedDamage = damage - 30
						char.Humanoid:TakeDamage(fixedDamage)
					elseif char:WaitForChild("Armor").Value == 2 then
						local fixedDamage = damage - 60
						char.Humanoid:TakeDamage(fixedDamage)

					elseif char:WaitForChild("Armor").Value == 3 then
						local fixedDamage = damage - 70
						char.Humanoid:TakeDamage(fixedDamage)
						
				end
				end
				
		end
		
		
		elseif char and char.Name ~= player.Name and hit.Name == "Left Arm" or hit.Name == "Left Leg" or hit.Name == "Right Arm" or hit.Name == "Right Leg" then
			if game:GetService("Players"):GetPlayerFromCharacter(char) == nil then
			char.Humanoid:TakeDamage(limbDMG)
			elseif game:GetService("Players"):GetPlayerFromCharacter(char) and game:GetService("Players"):GetPlayerFromCharacter(char).Team ~= player.Team then
				char.Humanoid:TakeDamage(limbDMG)

				end
		elseif char and hit.Name == "Head" and char.Name ~= player.Name then
			if game:GetService("Players"):GetPlayerFromCharacter(char) == nil then
			char.Humanoid:TakeDamage(100)
			elseif game:GetService("Players"):GetPlayerFromCharacter(char) and game:GetService("Players"):GetPlayerFromCharacter(char).Team ~= player.Team then

			char.Humanoid:TakeDamage(100)
		end
		end
		
	end

caster.RayHit:Connect(onrayHit)
--wait(0.2)
--whiz:Play()
game.Debris:AddItem(bullet, 25)
end)

this is my code so far, it fires and bullet and listens for when it has hit the target, i also added armor values to balance gameplay and stuff they dont work too but i think i could figure it out for myself after the team kill stuff is figured out

do something like:

if firedPlayer.Team == hitPlayer.Team then
    return
end

somewhere before any damage can be given

right after that put this:

local hitplayer = game.Players:GetPlayerFromCharacter(char)
if hitplayer and player.Team == hitplayer.Team then
    return
end

and maybe name the variable character instead of char (char is a built in function)

ive tried it and after like 50 shots or so it deals damage to the player on the same team still

Are you sure no one is switching teams between 50 shots?

yep no one is switching teams after 50 shots, its a random number or something when i tried it out it took like 2 magazines (noting that each mag has a 30 round cap) i can send a video, the only way ur switching teams is by pressing the change teams button or in the menu

Yes, I’d like a video
Also, maybe you shouldn’t put all the code inside the RemoteEvent function?

imma send you a video as soon as i can, about the remote event functuion it was the only way i could find to pút the player vars inside it whitout interfering whit other players stuff

1 Like

robloxapp-20241101-2117519.wmv (1.4 MB)
dont worry about the round timer and stuff they will be fixed soon

I didn’t see his health go down

oop forgot to mention that i was focused on something else mb
it appears to be a a issue whit the system preventing you from getting shot by your own bullets, i will look into that but its really rare, i tought the dummys whit my own name could still have the team values but ig im wrong

1 Like

Glad you found the answer to your problem. Happy developing! :grinning:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.