Ai following one player please help?
I´m creating a horror game, i created a mian script who works for all ai on a folder but i have a problem it follows only one player and ignore the others players
005 | local scaryimg = game.ReplicatedStorage:WaitForChild( "ScaryPlayer" ) |
007 | local candamage = true |
010 | local WanderX, WanderZ = 10 , 10 |
012 | function Added(Monsters) |
015 | function getHumanoid(model) |
016 | for _, v in pairs (model:GetChildren()) do |
017 | if v:IsA 'Humanoid' then |
023 | local zombie = Monsters |
025 | local human = getHumanoid(zombie) |
026 | local hroot = zombie:FindFirstChild( "HumanoidRootPart" ) |
027 | local zspeed = hroot.Velocity.magnitude |
030 | local pfs = game:GetService( "PathfindingService" ) |
032 | function GetPlayerNames() |
033 | local players = game:GetService( 'Players' ):GetChildren() |
035 | for _, v in pairs (players) do |
036 | if v:IsA 'Player' then |
037 | name = tostring (v.Name) |
042 | function GetPlayersBodyParts(t) |
045 | local figure = torso.Parent |
047 | for _, v in pairs (figure:GetChildren()) do |
054 | return "HumanoidRootPart" |
058 | function GetTorso(part) |
059 | local chars = game.Workspace:GetChildren() |
061 | for _, v in pairs (chars) do |
062 | if v:IsA 'Model' and v ~ = script.Parent and v.Name = = GetPlayerNames() then |
063 | local charRoot = v:FindFirstChild 'HumanoidRootPart' |
064 | if (charRoot.Position - part).magnitude < Distance then |
075 | local isWandering = 0 |
079 | while isWandering = = 0 do |
081 | local desgx, desgz = hroot.Position.x + math.random(-WanderX, WanderX), hroot.Position.z + math.random(-WanderZ, WanderZ) |
082 | human:MoveTo( Vector 3. new(desgx, 0 , desgz) ) |
083 | wait(math.random( 4 , 6 )) |
090 | local enemytorso = GetTorso(hroot.Position) |
091 | if enemytorso ~ = nil then |
093 | local function checkw(t) |
098 | if t [ ci ] = = nil and ci < #t then |
103 | return Vector 3. new( 1 , 0 , 0 ) + t [ ci ] |
110 | path = pfs:FindPathAsync(hroot.Position, enemytorso.Position) |
111 | waypoint = path:GetWaypoints() |
115 | local direct = Vector 3. FromNormalId(Enum.NormalId.Front) |
116 | local ncf = hroot.CFrame * CFrame.new(direct) |
118 | local rootr = Ray.new(hroot.Position, direct) |
119 | local phit, ppos = game.Workspace:FindPartOnRay(rootr, hroot) |
121 | if path and waypoint or checkw(waypoint) then |
122 | if checkw(waypoint) ~ = nil and checkw(waypoint).Action = = Enum.PathWaypointAction.Walk then |
123 | human:MoveTo( checkw(waypoint).Position ) |
127 | if checkw(waypoint) ~ = nil and checkw(waypoint).Action = = Enum.PathWaypointAction.Jump then |
129 | connection = human.Changed:connect( function () |
132 | human:MoveTo( checkw(waypoint).Position ) |
137 | hroot.Touched:connect( function (p) |
138 | local bodypartnames = GetPlayersBodyParts(enemytorso) |
139 | if p:IsA 'Part' and not p.Name = = bodypartnames and phit and phit.Name ~ = bodypartnames and phit:IsA 'Part' and rootr:Distance(phit.Position) < 5 then |
140 | connection = human.Changed:connect( function () |
149 | connection:Disconnect() |
153 | for i = 3 , #oldpoints do |
154 | human:MoveTo( oldpoints [ i ] .Position ) |
157 | elseif enemytorso = = nil and canWander then |
161 | human.MoveToFinished:Wait() |
167 | workspace.AiEnemys.ChildAdded:connect(Added) |
169 | for _,Monsters in pairs (workspace.AiEnemys:GetChildren()) do |
Help me please i need this urgently Thanks