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

How to damage NPC enemies and get KOs for them?

I've completed the animation process of my punch tool [as seen below], but can't get around damaging NPC's only when clicking and figuring out how to get a KO on the leaderboard after killing them. Keep in mind I'm using a "handless" tool.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local tool = script.Parent
local swingsound = tool:WaitForChild("Swing")


tool.Activated:connect(function()
	local choose = math.random(1,3)

	if choose == 1 then
		local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
		swingsound:Play()
		swing1animation:Play()
	elseif choose == 2 then
		local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
		swingsound:Play()
		swing2animation:Play()
   	elseif choose == 3 then
		local swing3animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch3)
		swingsound:Play()
		swing3animation:Play()
	end
end)

tool.Unequipped:connect(function()
	local hum = char:WaitForChild("Humanoid")
end)

2 comments

0

First, explain more, second... your problem is damaging the NPC or increasing the amount of KO's in your Leaderboard?

0

The problem I'm having is that when I use the punching tool, I want it to damage the NPC only when I left click. And the other problem I'm having is that I have no idea how to increase the amount of KOs on the leaderboard after I [successfully] kill an NPC (and I have a KO counter script already implemented into the NPC which gives a KO if I kill the NPC with another weapon, such as a sword).

1 answer

To deal damage to a target humanoid you want to a touched event, then check if what you touched has a humanoid inside of it.

LeftHand.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Number)
	end
end)

now to increase damage do

LeftHand.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(Number)
			if Hit.Parent:FindFirstChild("Humanoid").Health <= 0 then
				Player.leaderstats.KOS.Value = Player.leaderstats.KOS.Value + 1
		end
	end
end)

sorry if the code is slightly off, im writing on mobile

4 comments

0

Problem: Every time the left arm hits a part of a dead NPC it gives me a KO. So if I killed one NPC, but the torso, left arm, and right arm of the NPC hit my left arm, I'd get 3 KOs instead of one.

0

add a debounce

0

Awesome! Thanks man.

0

Oops. another problem. Whenever I play in-game the leftArm connect function conflicts with something so I can't damage the NPCs.

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