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

how to make sure i find the player?

Asked by 5 years ago

on line 15 is where i have trouble, sometimes the game takes some time to load and doesnt find the players character which leads to the code stopping. im not sure on using WaitForChild since something could lead to the error of "infinite yield possible"

any help?

game:GetService("Players")
local origin= script.Parent
local color= origin.Color
local origin_Pos = origin.Position

local player_List = nil
local pla_Being_Checked = nil                   
local pla_HumanRP= nil                         
local pla_Distance= nil
local pla_Pos
function player_Distance_Table_Check ()
    player_List= game.Players:GetChildren()
    for i = 1, #player_List do
        pla_Being_Checked = player_List[i]
        pla_HumanRP = pla_Being_Checked.Character:FindFirstChild("HumanoidRootPart")
        if pla_HumanRP then

            origin_Pos= origin.Position                            
            pla_Pos= pla_HumanRP.Position
            pla_Distance=(origin_Pos-pla_Pos)
            print("i found "..pla_HumanRP.Parent.Name.."hes "..pla_Distance.."studs away")

        end

    end 

end
while wait(4) do
player_Distance_Table_Check()
end
0
Just use WAitForChild iladoga 129 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

here u go i did a lot of changes it should work now

game:GetService("Players")
local origin= script.Parent
local color= origin.Color
local origin_Pos = origin.Position

local player_List = nil
local pla_Being_Checked = nil                   
local pla_HumanRP= nil                         
local pla_Distance= nil
local pla_Pos
function player_Distance_Table_Check ()
    player_List= game.Players:GetChildren()
    for i = 1, #player_List do
        pla_Being_Checked = player_List[i]
        pla_HumanRP = pla_Being_Checked.Character:FindFirstChild("HumanoidRootPart", true) -- the "a lot of " changing place
        if pla_HumanRP then

            origin_Pos= origin.Position                            
            pla_Pos= pla_HumanRP.Position
            pla_Distance=(origin_Pos-pla_Pos)
            print("i found "..pla_HumanRP.Parent.Name.."hes "..pla_Distance.."studs away")

        end

    end 

end
while wait(4) do
player_Distance_Table_Check()
end
Ad

Answer this question