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

(Solved)Does the for loop not work with the GetPlayers to recognize the player?

Asked by
brok4d 77
5 years ago
Edited 5 years ago

Well, that I do not go for the loop with GetPlayers () I give debug and jump to the end and stop. I've tried a simple for loop and it works.

local PathfindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players"):GetPlayers()
local enemy = script.Parent
local humanoidEnemy = enemy.Humanoid
local humanoidRootPart = enemy.HumanoidRootPart
local Start = script.Parent.Parent.Start
local Finish = script.Parent.Parent.Finish
local currentDestination = Start

for i , player in pairs(Players) do
    local character = game.Workspace:WaitForChild(player.Name)
    print(player.Name)
    if character then
        while wait(1) do
            local mag = (character.Head.Position - enemy.Head.Position).magnitude           
            if mag <= 50 then
                print("Attack")
            else
                print(mag)

                while wait(2) do
                    local startingPosition = humanoidRootPart.Position
                    local goalPosition = currentDestination.Position
                    local path = PathfindingService:FindPathAsync(startingPosition, goalPosition)
                    local waypoints = path:GetWaypoints()
                    for waypointIndex, waypoint in pairs(waypoints) do
                        local waypointPosition = waypoint.Position
                        humanoidEnemy:MoveTo(waypointPosition)
                        humanoidEnemy.MoveToFinished:Wait()
                    end 
                    if currentDestination == Start then
                        currentDestination = Finish
                    else
                        currentDestination = Start
                    end

                end
            end

        end
    end
end

0
you need to use :GetChildren() because otherwise for example, it wouldn't know if you meant :GetDescendants or :GetChildren() greatneil80 2647 — 5y
0
It does not work with any of those methods. Thank you brok4d 77 — 5y

2 answers

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
5 years ago

You're getting the players right when the server starts, which more often than not, the game doesn't see any players, yet. You'd have to give it a chance to at least get the 1st player. Also an FYI, this script will only be getting the first player that joins. To actively keep getting all players, you can use a PlayerAdded event to have this for each player everytime they join, or put the for loop in a while loop

local PathfindingService = game:GetService("PathfindingService")

local enemy = script.Parent
local humanoidEnemy = enemy.Humanoid
local humanoidRootPart = enemy.HumanoidRootPart
local Start = script.Parent.Parent.Start
local Finish = script.Parent.Parent.Finish
local currentDestination = Start
wait(2)
local Players = game:GetService("Players"):GetPlayers()

for i , player in pairs(Players) do
    local character = game.Workspace:WaitForChild(player.Name)
    print(player.Name)
    if character then
        while wait(1) do
            local mag = (character.Head.Position - enemy.Head.Position).magnitude           
            if mag <= 50 then
                print("Attack")
            else
                print(mag)

                while wait(2) do
                    local startingPosition = humanoidRootPart.Position
                    local goalPosition = currentDestination.Position
                    local path = PathfindingService:FindPathAsync(startingPosition, goalPosition)
                    local waypoints = path:GetWaypoints()
                    for waypointIndex, waypoint in pairs(waypoints) do
                        local waypointPosition = waypoint.Position
                        humanoidEnemy:MoveTo(waypointPosition)
                        humanoidEnemy.MoveToFinished:Wait()
                    end 
                    if currentDestination == Start then
                        currentDestination = Finish
                    else
                        currentDestination = Start
                    end

                end
            end

        end
    end
end


Ad
Log in to vote
0
Answered by
brok4d 77
5 years ago

Thank you, thank you very much, now I have the problem that it does not calculate the distance while advancing it stays in the distance from the beginning.

while true do
            local mag = (character.Head.Position - enemy.Head.Position).magnitude           
            if mag <= 50 then
                print("Attack")
            else
                print(mag)

                while wait(2) do
                    local startingPosition = humanoidRootPart.Position
                    local goalPosition = currentDestination.Position
                    local path = PathfindingService:FindPathAsync(startingPosition, goalPosition)
                    local waypoints = path:GetWaypoints()
                    for waypointIndex, waypoint in pairs(waypoints) do
                        local waypointPosition = waypoint.Position
                        humanoidEnemy:MoveTo(waypointPosition)
                        humanoidEnemy.MoveToFinished:Wait()
                    end 
                    if currentDestination == Start then
                        currentDestination = Finish
                    else
                        currentDestination = Start
                    end

                end
            end
            wait()
        end
0
Sorry can Break and coarse brok4d 77 — 5y

Answer this question