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

How can I damage npc and players?

Here it is only dealing damage to players, but I want to damage npc too, if only npc is around me it will only damage him, if it has the player and npc will deal damage together. And the same for the player.

~LocalModuleScript Damage

local plrs = game.Players:GetPlayers()
		table.insert(plrs, game.Workspace.Map.Npcs.Bandit:GetChildren())
		-- End dark particles
	
for i, v in pairs(plrs) do
		
		local char = v.Character
		
		if char then
			
			if v.Name ~= player.Name and (char.HumanoidRootPart.Position - pos).magnitude <= size/2 and math.abs(char.HumanoidRootPart.Position.Y - pos.Y) <= 7 then
				
				affected[#affected + 1] = v
				
			elseif v.Name ~= player.Name and (char.HumanoidRootPart.Position - pos).magnitude <= size then
				
				lessAffected[#lessAffected + 1] = v
				
			end
			
		end
		
	end
	
	--
	
	for i, v in pairs(affected) do
		
		local char = v.Character
		
		if char then
-- damage
for o = 1, 30 * speed do
						
char.HumanoidRootPart.CFrame = char.HumanoidRootPart.CFrame - Vector3.new(0, speed/30, 0)
						
char.Humanoid.Health = char.Humanoid.Health - (damage / (30 * speed))
wait(1/30)
						
					end
			
			end
		
	end

-- Edit/ I just remembered that too.

~Script Damage

local char = player.Character

for i, v in pairs(char:GetChildren()) do
	
	if v:IsA("BasePart") then
		
		coroutine.resume(coroutine.create(function() --ModuleScript
			
			local deb = false
			
			v.Touched:Connect(function(hit)
				
				if game.Players:FindFirstChild(hit.Parent.Name) and not deb then
--Script

2 answers

Before you loop all the players, make a variable off of the players like this:

local players = game.Players:GetPlayers()

Then you have to add your npcs to that list of players so that they also go through the loop and can take damage:

table.insert(players, game.Workspace.Npc) -- Change with whatever your npc is named

Edit: And don't forget of course to change your first loop: for i, v in pairs(players) do

3 comments

0

"local char = v.Character" Character is not a valid member of Model Local of npcs "game.Workspace.Npcs.Bandit" --(.Bandit and .BanditBoss)

0

"table.insert(plrs, game.Workspace.Map.Npcs.Bandit:GetChildren())" There is no error, but the npcs are not getting damaged yet, maybe it's a script error I just added.

0

Ah my bad you'd have to rewrite it slightly cause you've got a mix of players and characters now, you should add the character models of all players instead, not the players directly

You can use the function of the Humanoid which is :TakeDamage(). It's quite simple. In the brackets you will put the amount of health that you want to reduce from the player.

2 comments

0

This script is only catching the players, I want the npc too, the damage part is working only on players, I just don't know what to do to get the 2

0

The first line is the problem because you're looking for players and ignoring npc. ~Dmg "Line 33"

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