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

how do i make my knife only kill npcs aka zombies?

ive been trying to figure out how to make my knife only kill npcs and not people but can't seem to figure out how to do it after attempting many solutions.

local Handle = script.Parent; 
local Tool = Handle.Parent; 
local Config = Handle:WaitForChild("Configuration"); 
local Sounds = Config:WaitForChild("Sounds"); 
local StabE = Config:WaitForChild("StabE"); 
local ThrowE = Config:WaitForChild("ThrowE"); 
local Throw = false; 

function Throw(Target) 
	local Flying = false;
	local NewBlade = Handle:Clone(); 
	NewBlade.Parent = workspace; 
	NewBlade.Anchored = false;
	NewBlade.CanCollide = false; 
	NewBlade.Locked = true; 
	NewBlade.Transparency = 0; 
	local CloneFly = Sounds.Fly:Clone()
	local CloneHit = Sounds.Hit:Clone()
	local CloneTing = Sounds.Ting:Clone()
	
	CloneHit.Parent = NewBlade
	CloneFly.Parent = NewBlade
	CloneFly:Play()
	
	NewBlade.Velocity = (Target - NewBlade.CFrame.p).unit * 100; 
	NewBlade.CFrame = CFrame.new(NewBlade.CFrame.p, NewBlade.CFrame.p + NewBlade.Velocity) * CFrame.Angles(0, 0, math.rad(-90)); 
	
	local FlyForce = Instance.new('BodyForce', NewBlade);
	FlyForce.force = Vector3.new(0, 196.2 * NewBlade:GetMass() * 0.98, 0);
	local Spiral = Instance.new('BodyAngularVelocity', NewBlade); of the blade as it flies
	Spiral.angularvelocity = NewBlade.CFrame:vectorToWorldSpace(Vector3.new(0, -400, 0));
	
	NewBlade.Touched:Connect(function(hit) 
		if hit.Parent:FindFirstChild("Humanoid") and Tool.Parent ~= hit.Parent then 
			CloneHit:Play();
			local Hum = hit.Parent.Humanoid;
			Hum.Health = 0; 
			NewBlade:Destroy(); 
			print("Blade cleaned!");
		elseif not hit.Parent:FindFirstChild("Humanoid") and not hit.Parent.Parent:FindFirstChild("Humanoid") then
			CloneTing:Play();
			NewBlade:Destroy(); 
			print("Blade cleaned!");
		end
	end)
end

function Stab()
	local Blade = Handle;
	
	Blade.Touched:Connect(function(hit)
		wait();
		if hit.Parent:FindFirstChild("Humanoid") and Tool.Parent ~= hit.Parent then 
			wait();
			Sounds.Hit:Play();
			local Hum = hit.Parent.Humanoid; 
			Hum.Health = 0; 
			print("Player killed!");
		elseif not hit.Parent:FindFirstChild("Humanoid") and not hit.Parent.Parent:FindFirstChild("Humanoid") then
			wait();
			Sounds.Ting:Play();
			print("Hit the wall!")
		end
	end)
end


ThrowE.OnServerEvent:Connect(function(Throwing, Start)
	local Target = Start[2]
	Throw(Target)
end)
StabE.OnServerEvent:Connect(function()
	Stab()
end)

2 comments

0

No, don't nest events inside functions. Events are asynchronous.

0

uh, what?

2 answers

✓ Accepted answer

Try this in place of Line # 53:

if hit.Parent:FindFirstChild("Humanoid") ~= nil and not game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) then

3 comments

0

I'm sorry, I mean't line #53, I read the script wrong. Try 53, instead of 34.

0

its alr, it works, thanks

0

No problem :)!

You would have to make the script verify if it's a zombie. A simple way would be to check the Zombie's name

At line 34 instead of

if hit.Parent:FindFirstChild("Humanoid") and Tool.Parent ~= hit.Parent then

You would check if the person being hit is a zombie, so let's say your Zombie name is just "Zombie" then it would become

if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name == "Zombie" then

Obviously no one in your game is named Zombie, so this would make so only zombies are damaged.

Hope I could be of help! Have a great day.

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