#I made a script that makes npc follow a player but it acts weird

83 messages · Page 1 of 1 (latest)

sacred jay
#

If npc's speed is bigger that player's, it just does this strange thing like in the video, here's the code I used:

local Players = game:GetService("Players")
local char = script.Parent
local maxaggrorange = 999
local targetdistance = maxaggrorange
local debounce = false

while true do
    for i, player in Players:GetPlayers() do
        if player.Character then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            if (char.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= targetdistance then
                targetdistance = (char.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
                if targetdistance <= maxaggrorange then
                    
                    char.EnemyHumanoid:MoveTo(player.Character.PrimaryPart.Position)
                
            end
        end
        end
        end
    targetdistance = maxaggrorange
    task.wait()
end
dusky hound
sacred jay
#

And that's it?

dusky hound
sacred jay
#

That didn't seem to help

dusky hound
proper vine
sacred jay
#

OBS

dim hawk
sacred jay
#

It's hard to describe but the npc doesn't chase the player properly

proper vine
#

Well the npc does walk funny

sacred jay
#

I think the problem is that it doesn't update the player's position properly

sharp nicheBOT
#

studio** You are now Level 1! **studio

dim hawk
sacred jay
#

Can I get a little more details about the minimum distance?

dim hawk
sacred jay
#

Ok, now I got it, I'll try that

#

I think that I have a different problem, it's not about the npc trying to jump but about it not being able to reach me even if it has bigger walkspeed than me

sacred jay
#

Yeah, this problem might be client sided

#

Let me check

#

Yep

#

Then I should make the chase script client sided but the npc should also move from the server's perspective

#

Complicated

dim hawk
#

just take the time between ticks, current velocity of the player and velocity vector. Then go to where the player will be, not where he is

sacred jay
#

Wait there's some thing called network ownership meybe that is the thing

sharp nicheBOT
#

studio** You are now Level 2! **studio

sacred jay
#

If I use SetNetworkOwner, should I set it to HumanoidRootPart or every part of the model?

sacred jay
#

Setnetworkowner made it worse

#

I needed to make client to handle the chase but it made this problem appear on the server side

dim hawk
sacred jay
#

It did the opposite of what I wanted

dim hawk
#

if so then of course that will make it worse as you're trying to change the network of that npc 60 times per second

sacred jay
#

Setnetworkowner may not be a solution I guess

sacred jay
#

I don't understand anything, first it's working correctly on the server but then it becomes the same as on the client (example of setnetworkowner not being used)

shell field
#

instead of while true do

#

do while wait(.1) do

sacred jay
#

I already did runservice.heartbeat instead

shell field
#

did it work

sacred jay
#

As you can see no

shell field
#

what i would do

#

lemme see

#

hmm

sacred jay
#

Latest version:

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local char = script.Parent
local maxaggrorange = 999
local mindistance = 0
local targetdistance = maxaggrorange
local debounce = false
local targetplayer

RunService.Heartbeat:Connect(function()
    for i, player in Players:GetPlayers() do
        if player.Character then
            local humanoid = player.Character:FindFirstChild("Humanoid")
            if (char.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= targetdistance then
                targetdistance = (char.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
                if targetdistance <= maxaggrorange --[[and targetdistance >= mindistance]] then
                    if targetplayer ~= player then
                        targetplayer = player
                        --[[for i,v in char:GetChildren() do
                            if v:IsA("BasePart") then
                                v:SetNetworkOwner(targetplayer)
                            end
                        end]]
                    end
                    char.EnemyHumanoid:MoveTo(player.Character.PrimaryPart.Position)
                else
                end
            end
        end
    end
    targetdistance = maxaggrorange
    --task.wait()
end)

shell field
#
while wait(.1) do
    local character = script.Parent
    local humanoid = character:WaitForChild('Humanoid')
    local player = game.Players:WaitForChild('CreativeCha0s')
    local char = player.Character
    repeat
        wait()
         char = player.Character
    until
    char ~= nil
    local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
    humanoid:MoveTo(humanoidRootPart.Position)
end
#

put a script into your rig

#

and replace my name with your name

#

see if this works

#

if it does now just run a for loop to see the nearest player or whatever

sacred jay
#

I doubt this will work but I'll try it still

shell field
#

never doubt it fr

sacred jay
#

It doesn't

shell field
#

oh

#

it works for me

#

@sacred jay it works for me

sacred jay
#

Good to you

shell field
#

want me to share you the model?

#

@sacred jay

sacred jay
#

Idk

shell field
#

ok

sacred jay
#

Set it's walkspeed to like 30

sharp nicheBOT
#

studio** You are now Level 3! **studio

sacred jay
#

And maybe screensharing is more convenient than recording the video?

shell field
#

thats gonna break it because the npc is faster than the player...

#

but if i set my walkspeed to 30 it will fix it

sacred jay
#

That's the point

shell field
#

so why is the npcs walkspeed so high?

#

whats the point of that

sacred jay
#

I need to make a sort of glass cannon

shell field
#

so why cant u have the npc at a normal speed

sharp nicheBOT
#

studio** You are now Level 12! **studio

shell field
#

i fixed it

#

light work

sacred jay
#

It still doesn't reach the player on your vid

#

It justs copies your walkspeed

shell field
#

are you trying to make it kill me?

sacred jay
#

I want the npc to touch the player

shell field
#

ok

#

yeah i give up

sacred jay
#

Me too