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

Why does my NPC script refuse to work on a server?

Asked by 9 years ago

Hi guys, I have finished coding my NPC script. I have made it so the NPC is local (inside the CurrentCamera) and have used pathfinding to make the character move around in an random part of an area (outlined by an invisible brick.)

It works when I use it in Play Solo, but when I test it in a server, it refuses to work. Nothing is printed or errors out, even if I add prints to my code. I don't understand why it doesn't work. I even tried my code in a LocalScript with little success.

Here is my script:

math.randomseed(tick())

local paths = game.Lighting.Paths:GetChildren()
local npc = script.Parent
local hum = npc.Humanoid
local torso = npc.Torso
local head = npc.Head
local dialog = head.ChatDialog
local player

function RandomiseLocation(part)
    local function r(a,b)
        return a + (b-a) * math.random()
    end
    local c1 = part.Position + (part.Size/2)
    local c2 = part.Position - (part.Size/2)
    return Vector3.new(r(c1.X,c2.X),r(c1.Y,c2.Y),r(c1.Z,c2.Z))
end

player = npc.Player.Value
while true do
    wait()
    print("Doing eet")
    if dialog.InUse ~= true then
        wait()
        hum.WalkSpeed = 16
        print("Doing eet2")
        local rand = math.random(1,#paths)
        local randLoc = RandomiseLocation(paths[rand])
        print("Doing eet3")
        if (randLoc - torso.Position).magnitude < 10 then repeat wait() randLoc = RandomiseLocation(paths[rand]) until (randLoc - torso.Position).magnitude >= 10 end
        randLoc = Vector3.new(randLoc.X, torso.Position.Y, randLoc.Z)
        print(randLoc.X, randLoc.Y, randLoc.Z)
        local path = game:GetService("PathfindingService"):ComputeRawPathAsync(torso.Position,randLoc,500)
        if path.Status == Enum.PathStatus.Success then
            local points = path:GetPointCoordinates()
            for _,pos in pairs(points) do
                hum:MoveTo(pos)
                repeat wait() until (pos - torso.Position).magnitude <= 3
            end
            hum:MoveTo(randLoc)
            local speed = hum.Running:wait()
            if speed ~= 0 then repeat if dialog.InUse ~= true then speed = hum.Running:wait() end until speed == 0 end
            wait(.25)
        end
    else
        wait()
        hum.WalkSpeed = 0
        torso.CFrame = CFrame.new(torso.Position, Vector3.new(player.Character.Torso.Position.X,torso.Position.Y,player.Character.Torso.Position.Z))
    end
end

Any help is appreciated, thanks!

Answer this question