Ad
Log in to vote
0

How can I make this script target Zombies instead of players?

Asked by 7 months ago

I am trying to get this script to function when a zombie gets too close to a humanoid that has this script the humanoid will swing their weapon at the zombie, the only problem is that I don't know how to make the script target the zombies instead of players. After putting about an hours work into this I decided to come here for help, here is the part of the script that I am having trouble with

while true do
    local p = game.Players:GetChildren()
    for i = 1,#p do
        if p[i].Character~=nil then
            if p[i].Character:findFirstChild("Torso")~=nil then
                if (p[i].Character.Torso.Position - script.Parent.Torso.Position).magnitude < 5 then
                    local anim = Instance.new("StringValue")
                    anim.Value = possibleAnims[math.random(1, #possibleAnims)]
                    anim.Name = "toolanim"
                    anim.Parent = script.Parent.LinkedSword
                end
            end
        end
    end
    wait(0.5)
end

Hi.

Looks like you're using an ad blocker.

That's fine...

No, it really is.

Just, I mean, we put a lot of work into this site, you know?

It would be really really appreciated if you would turn off your ad blocker for our website...

We tried really hard to make our ads as unobtrusive as possible.

If you really hate ads, would you consider a $2 donation via Patreon?

Here's the link.

We love you. We hope you love us too.

.

..

...

Now back to your regularly scheduled Scripting Helpers....

...

..

.

2 answers

Log in to vote
0
Answered by 7 months ago
function swing()
     local anim = Instance.new("StringValue")
          anim.Value = possibleAnims[math.random(1, #possibleAnims)]
           anim.Name = "toolanim"
           anim.Parent = script.Parent.LinkedSword
end

while true do
    local p = game.Workspace:GetChildren()
    for i = 1,v in pairs(p) do
        if v.Name == "Zombie" then
        if (v.Torso.Position-script.Parent.Torso.Position).magnitude <= 5 then
                    swing()
        end
        end
    end
    wait(0.5)
end

Instead of cycling through the children of the Players instance, it cycles through the workspace though it may take a minute so I'd advice putting all of the zombies under a folder in workspace so the script doesn't have to go through every part,model,etc in the game. Really helps decrease lag.

0
Why is your tabbing so bad? User#15006 30 — 7mo
Ad
Log in to vote
0
Answered by
Jijah 0
7 months ago

You can discern NPCs from players using the PlayerFromCharacter() void as well. I believe it can be used on any model. If PlayerFromCharacter() returns nil when used, it means that the model in question is not controlled by a player, thus the model is either an NPC or an inanimate object.

Answer this question



Ad