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

Need Players To Be Able To Kill Humanoids But Not Each Other?

Ok. So here goes. This script is located in a gun. As of right now players CAN Kill Zombie Humanoids But can also kill each other. I've tried uncommenting the lines for anti team kill within the script located on line 8, 116, and 118 but everytime I do I get an infinite yield possible warning. I have tried to modify this script but have had no luck.

So all in all I need this to be able to kill my zombie Humanoids but not kill other players. Also I do have a team created just for the players called Humans, dont know if that helps but I thought I should let you guys know just in case.

Also the code is longer than this but I couldnt paste it all. However I posted the beginning up ti the function Fire as this is the important part



local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = Workspace.CurrentCamera
local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild("Humanoid")
--local TEAM = Character:WaitForChild("Team")
local VisualizeBullet = game.ReplicatedStorage:WaitForChild("VisualizeBullet")
local Module = require(Tool:WaitForChild("Setting"))
local GunScript_Server = Tool:WaitForChild("GunScript_Server")
local ChangeAmmoAndClip = GunScript_Server:WaitForChild("ChangeAmmoAndClip")
local InflictTarget = GunScript_Server:WaitForChild("InflictTarget")
local AmmoValue = GunScript_Server:WaitForChild("Ammo")
local ClipsValue = GunScript_Server:WaitForChild("Clips")
local GUI = script:WaitForChild("GunGUI")
local IdleAnim
local FireAnim
local ReloadAnim
local ShotgunClipinAnim
local Grip2
local Handle2
local HandleToFire = Handle

if Module.DualEnabled then
	Handle2 = Tool:WaitForChild("Handle2",2)
	if Handle2 == nil and Module.DualEnabled then error("\"Dual\" setting is enabled but \"Handle2\" is missing!") end
end

local Equipped = false
local Enabled = true
local Down = false
local Reloading = false
local AimDown = false
local Ammo = AmmoValue.Value
local Clips = ClipsValue.Value
local MaxClip = Module.MaxClip
local PiercedHumanoid = {}

if Module.IdleAnimationID ~= nil or Module.DualEnabled then
	IdleAnim = Tool:WaitForChild("IdleAnim")
	IdleAnim = Humanoid:LoadAnimation(IdleAnim)
end
if Module.FireAnimationID ~= nil then
	FireAnim = Tool:WaitForChild("FireAnim")
	FireAnim = Humanoid:LoadAnimation(FireAnim)
end
if Module.ReloadAnimationID ~= nil then
	ReloadAnim = Tool:WaitForChild("ReloadAnim")
	ReloadAnim = Humanoid:LoadAnimation(ReloadAnim)
end
if Module.ShotgunClipinAnimationID ~= nil then
	ShotgunClipinAnim = Tool:WaitForChild("ShotgunClipinAnim")
	ShotgunClipinAnim = Humanoid:LoadAnimation(ShotgunClipinAnim)
end

function wait(TimeToWait)
	if TimeToWait ~= nil then
		local TotalTime = 0
		TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
		while TotalTime < TimeToWait do
			TotalTime = TotalTime + game:GetService("RunService").Heartbeat:wait()
		end
	else
		game:GetService("RunService").Heartbeat:wait()
	end
end

function RayCast(Start,Direction,Range,Ignore)
	local Hit,EndPos = game.Workspace:FindPartOnRay(Ray.new(Start,Direction*Range),Ignore)
	if Hit then
		if (Hit.Transparency > 0.75
			or Hit.Name == "Handle"
			or Hit.Name == "Effect"
			or Hit.Name == "Bullet"
			or Hit.Name == "Laser"
			or string.lower(Hit.Name) == "water"
			or Hit.Name == "Rail"
			or Hit.Name == "Arrow"
			or (Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health == 0)
			or (Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent:FindFirstChild("TEAM") and TEAM and Hit.Parent.TEAM.Value == TEAM.Value))
			or (Hit.Parent:FindFirstChild("Humanoid") and PiercedHumanoid[Hit.Parent.Humanoid]) then
			Hit,EndPos = RayCast(EndPos+(Direction*.01),Direction,Range-((Start-EndPos).magnitude),Ignore)
		end
	end
	return Hit,EndPos
end

function ShakeCamera()
	if Module.CameraShakingEnabled then
		local Intensity = Module.Intensity/(AimDown and Module.MouseSensitive or 1)
		for i = 1, 10 do
			local cam_rot = Camera.CoordinateFrame - Camera.CoordinateFrame.p
			local cam_scroll = (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude
			local ncf = CFrame.new(Camera.Focus.p)*cam_rot*CFrame.fromEulerAnglesXYZ((-Intensity+(math.random()*(Intensity*2)))/100, (-Intensity+(math.random()*(Intensity*2)))/100, 0)
			Camera.CoordinateFrame = ncf*CFrame.new(0, 0, cam_scroll)
			wait()
		end
	end
end

function Fire(ShootingHandle)
	local PierceAvailable = Module.Piercing
	PiercedHumanoid = {}
	if FireAnim then FireAnim:Play(nil,nil,Module.FireAnimationSpeed) end
	if not ShootingHandle.FireSound.Playing or not ShootingHandle.FireSound.Looped then ShootingHandle.FireSound:Play() end
	local Start = (Humanoid.Torso.CFrame * CFrame.new(0,1.5,0)).p--(Handle.CFrame * CFrame.new(Module.MuzzleOffset.X,Module.MuzzleOffset.Y,Module.MuzzleOffset.Z)).p
	local Spread = Module.Spread*(AimDown and 1-Module.SpreadRedution or 1)
	local Direction = (CFrame.new(Start,Mouse.Hit.p) * CFrame.Angles(math.rad(-Spread+(math.random()*(Spread*2))),math.rad(-Spread+(math.random()*(Spread*2))),0)).lookVector
	while PierceAvailable >= 0 do
		local Hit,EndPos = RayCast(Start,Direction,5000,Character)
		if not Module.ExplosiveEnabled then
			if Hit and Hit.Parent then
				local TargetHumanoid = Hit.Parent:FindFirstChild("Humanoid")
				local TargetTorso = Hit.Parent:FindFirstChild("Torso")
				--local TargetTEAM = Hit.Parent:FindFirstChild("TEAM")
				if TargetHumanoid and TargetHumanoid.Health > 0 and TargetTorso then
					--if TargetTEAM and TargetTEAM.Value ~= TEAM.Value then
						InflictTarget:FireServer(TargetHumanoid,
												TargetTorso,
												(Hit.Name == "Head" and Module.HeadshotEnabled) and Module.BaseDamage * Module.HeadshotDamageMultiplier or Module.BaseDamage,
												Direction,
												Module.Knockback,
												Module.Lifesteal,
												Module.FlamingBullet)
						PiercedHumanoid[TargetHumanoid] = true
					--end
				else
					PierceAvailable = 0
				end
			end


Narrowed Down Code:

function Fire(ShootingHandle)
	local PierceAvailable = Module.Piercing
	PiercedHumanoid = {}
	if FireAnim then FireAnim:Play(nil,nil,Module.FireAnimationSpeed) end
	if not ShootingHandle.FireSound.Playing or not ShootingHandle.FireSound.Looped then ShootingHandle.FireSound:Play() end
	local Start = (Humanoid.Torso.CFrame * CFrame.new(0,1.5,0)).p--(Handle.CFrame * CFrame.new(Module.MuzzleOffset.X,Module.MuzzleOffset.Y,Module.MuzzleOffset.Z)).p
	local Spread = Module.Spread*(AimDown and 1-Module.SpreadRedution or 1)
	local Direction = (CFrame.new(Start,Mouse.Hit.p) * CFrame.Angles(math.rad(-Spread+(math.random()*(Spread*2))),math.rad(-Spread+(math.random()*(Spread*2))),0)).lookVector
	while PierceAvailable >= 0 do
		local Hit,EndPos = RayCast(Start,Direction,5000,Character)
		if not Module.ExplosiveEnabled then
			if Hit and Hit.Parent then
				local TargetHumanoid = Hit.Parent:FindFirstChild("Humanoid")
				local TargetTorso = Hit.Parent:FindFirstChild("Torso")
				--local TargetTEAM = Hit.Parent:FindFirstChild("TEAM")
				if TargetHumanoid and TargetHumanoid.Health > 0 and TargetTorso and game:GetService('Players'):GetPlayerFromCharacter(Humanoid.Parent) == nil then
					--if TargetTEAM and TargetTEAM.Value ~= TEAM.Value then
						InflictTarget:FireServer(TargetHumanoid,
												TargetTorso,
												(Hit.Name == "Head" and Module.HeadshotEnabled) and Module.BaseDamage * Module.HeadshotDamageMultiplier or Module.BaseDamage,
												Direction,
												Module.Knockback,
												Module.Lifesteal,
												Module.FlamingBullet)
						PiercedHumanoid[TargetHumanoid] = true
					--end
				else
					PierceAvailable = 0
				end
			end

1 answer

✓ Accepted answer

This code is way to long to re-write for you but a way to tell if a Humanoid belongs to a player or a NPC is to use GetPlayerFromCharacter() which is part of the players service. For example:

if game:GetService('Players'):GetPlayerFromCharacter(humanoid.Parent) then
	print('This is a player')
else
	print('This is a NPC')
end

More info on this function can be found here: http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter

1 comment

0

I added it into the code but no matter what it either kills everyone (meaning the zombies and the players) or no one at all.. I added the snippet of code up top. I narrowed it down now (Called Narrowed down Code). Also I added your code into it and as I said it either kills all or none. Your code was added into the if statement on line 16. 

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