Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DistanceFromCharacter does not seem to work at all?

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

2 comments

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.

0

Thanks.

1 answer

✓ Accepted answer

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

2 comments

0

Thanks.

0

Np!

This discussion is preserved as it was. New answers, comments, and votes are closed.