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

Script not working to make the NPC move to a specific part?

Asked by
uhjos_h 19
3 years ago

Basically, I have tried to make an NPC that moves to a specific part, basically it works, but it doesn't move the NPC at the right time, basically 3 seconds in.

local timeend = false
local npc = script.Parent
wait (14)
timeend = true
if timeend == true then
    npc.Humanoid:MoveTo(workspace.yesyes.Position)
end

I have looked on things like Youtube, the DevForum, and here for a solution and still cannot manage to get it to work.

2 answers

Log in to vote
0
Answered by 3 years ago

Hey! Sorry I answered a bit late but I have a thought that might be somewhat useful.

So basically, every set amount of time, a object (or npc in your case will move to a set location every time it gets told too, it also moves furthest away from the player(s) and doesn't move to the same space 2 times in a row.

SETUP: Figure it out I'm too lazy sorry i'm doing a skribbl.io tournament lol

Here:

local npcModel = game.Workspace.Crate1
local npcLocationFolder = game.Workspace.GameFolder.DepositFolder
local SET_TIME = 14

local npcLocations = crateLocationFolder:GetChildren()

-- Moves the NPC to locationPart's position
local function moveNPCToPart(locationPart)
    npcModel:SetPrimaryPartCFrame(locationPart.CFrame)
end

local function getTotalPlayerDistanceFromPart(locationPart)
    local totalDistance = 0

    local players = game.Players:GetPlayers()
    for i = 1, #players do
        local player = players[i]
        local distance = player:DistanceFromCharacter(locationPart.Position)
        totalDistance = totalDistance + distance
    end

    return totalDistance
end

local function changeNPCPosition()
    local greatestDistance = 0
    local locationPartPicked

    for i = 1, #npcLocations do
        local locationPart = npcLocations[i]
             -- if locationPart.Name ~= nameValue.Value then
            local distance = getTotalPlayerDistanceFromPart(locationPart)
            if distance >= greatestDistance then
                greatestDistance = distance
                locationPartPicked = locationPart
            end
        end
    end

    moveNPCToPart(locationPartPicked)
end


while true do
    wait(SET_TIME)
    changeNPCPosition()
end

if it breaks just message me and i will see whats wrong cya :D

Ad
Log in to vote
0
Answered by
uhjos_h 19
3 years ago

Never mind, it works! Feel free to use this code in your game, I just counted the wait time wrong.

Answer this question