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

Why does this script not work?

Asked by 10 years ago

I have this script for a zombie to find the closest person and chase him, but the findNearestTarget() function is not working. there is nothing in the output, please help!!

local player = script.Parent
local followingPath = false
torso = script.Parent.Torso
range = 500
target = nil

function findNearestTarget()
    for _, plr in pairs(game.Players:GetPlayers()) do
        if plr.Character ~= nil then
            tor = plr.Character.Torso
            if target ~= nil then
                if not target or (torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
                        target = plr.Character
                    break
                end
            end
        end
    end
end

while true do
    wait(.1)
    findNearestTarget()
    if target~=nil then
        script.Parent.tar.Value = Vector3.new(target.Torso.Position)
    end
end

while true do

    if not followingPath then
        followingPath = true
    path = game:GetService("PathfindingService"):ComputeRawPathAsync(player.Torso.Position, script.Parent.tar.Value, 500)
    points = path:GetPointCoordinates()

    game.Workspace.Points:ClearAllChildren()

    for p = 1, #points do
        local part = Instance.new("Part")
        part.Size = Vector3.new(1, 1, 1)
        part.Position = points[p]
        part.Anchored = true
        part.CanCollide = false
        part.Parent = game.Workspace.Points
    end

    for _, point in pairs(points) do
        script.Parent.Zombie:MoveTo(point)
        repeat
            distance = (point - player.Torso.Position).magnitude
            wait()
        until distance < 3
    end
    followingPath = false
    end
    wait(0.1)
end


2 answers

Log in to vote
1
Answered by 10 years ago

Replace Line 12 with this:

if not target or (torso.Position-tor.Position).magnitude < (torso.Position-target.Torso.Position).magnitude then
0
its still not updating tar alexdogg11 5 — 10y
0
if you want, I made the model free to get in roblox in my models if you wanna take it and try to figure out whats wrong with it alexdogg11 5 — 10y
Ad
Log in to vote
0
Answered by 10 years ago

i just made a free model for someone in the group to use its a alien that can chase and kill and be killed with guns / swords. u can change the looks to make it look like a zombie , i hope this helps.

http://www.roblox.com/alien-for-kid-item?id=174207024

Answer this question