Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I damage npc and players?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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

0
"local char = v.Character" Character is not a valid member of Model Local of npcs "game.Workspace.Npcs.Bandit" --(.Bandit and .BanditBoss) ToddyWars 41 — 4y
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. ToddyWars 41 — 4y
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 User#834 0 — 4y
Ad
Log in to vote
0
Answered by
bluzorro 417 Moderation Voter
4 years ago
Edited 4 years ago

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.

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 ToddyWars 41 — 4y
0
The first line is the problem because you're looking for players and ignoring npc. ~Dmg "Line 33" ToddyWars 41 — 4y

Answer this question