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

Can anyone tell me why my custom npc won't move?

Asked by 4 years ago
Edited 4 years ago

I have a slime and I'm trying to make it move to the Player's position but when it runs the code below nothing happens except for a lag spike every 7 seconds.

wait(0.2)
while true do
    script.Parent:FindFirstChild("Range").Touched:Connect(function(other)
        if other.Parent:FindFirstChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(other.Parent)
            if player then
                wait(7)
                script.Parent.Humanoid:MoveTo(player.Humanoid.Position)
            end
        end
    end)
    wait(0.2)
end

MoveTo functions works with a vector3 but when I try to input the player's coordinates it breaks.

P.S. I added a humanoid to a model I made without a humanoid. I also added the Torso, Head, and root part. Maybe I stuffed up there?

Thanks

1 answer

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

So firstly, I'm just going to say that using the Roblox Humanoid class for something like a Slime is pretty obsolete compared to all the bountiful other methods Roblox is capable of now, so I would use something else... but I'm not sure what would be best for your specific slime model.

Anyways, one problem I already see is that the loop takes 7.2 seconds to repeat each time, which is quite slow and will almost guarantee the slime will never catch the player unless the player has some sort of unfulfilled death wish. The second problem I see is that "Position" is not a valid property of the Humanoid class. Both R6 and R15 Roblox rigs have a part called "HumanoidRootPart", so I would use that as a basis on the player's position. I would write something like this:

local run_service = game:GetService("RunService");

script.Parent:WaitForChild("Range").Touched:Connect(function(other)
    while true do
        if other.Parent:FindFirstChild("Humanoid") then
            local char = other.Parent;
            if char:FindFirstChild("HumanoidRootPart") then
               script.Parent.Humanoid:MoveTo(other.HumanoidRootPart.Position);
        end
        end
        run_service.Heartbeat:Wait();
    end
end)

Be wary of the run_service.Heartbeat:Wait() though as it will lag your server if used too much. Cheers :)

0
Thank you so much! It works great and I'll look into other methods of moving the enemy that don't involve the Humanoid class. NaNoBOT3000AND1 2 — 4y
0
I'm glad I could help @NaNo :) Loughdough 291 — 4y
Ad

Answer this question