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

DistanceFromCharacter does not seem to work at all?

Asked by 6 years ago
Edited 6 years ago

I have this script, which is in a folder, which is in a part in workspace. When I click on play, nothing happens at all and the output remains empty. It does not even print out 0. May someone please tell me what's wrong. This is my script.

for _, player in pairs(game:GetService("Players"):GetPlayers()) do
    print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
end
0
When you click play, the server loads up, your script runs. Then, your character spawns in. So when this script runs, game.Players has nothing in it. Hook it up to the PlayerAdded event or wait() a little bit before the loop. theCJarmy7 1293 — 6y
0
Thanks. SweetNoodleSoup 14 — 6y

1 answer

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

Chances are, it's because the character isn't loaded yet.

Do this instead and you should get some output:

while true do -- loop it up boiiz
    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
        print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
    end
    wait(1) -- ain't nobody got time 4 overflow
end

Or:

game:GetService("Players").PlayerAdded:Connect(function(player) -- when a player joins
    player.CharacterAdded:connect(function() -- when le character pops in
        for _, player in pairs(game:GetService("Players"):GetPlayers()) do
            print(player:DistanceFromCharacter(Vector3.new(0, 0, 0)))
        end
    end
end
0
Thanks. SweetNoodleSoup 14 — 6y
0
Np! sweetkid01 176 — 6y
Ad

Answer this question