Am I using player:DistanceFromCharacter right?

I was in the works of making a script, so I was doing some checks every once in a while to make sure the script was working. (surprise, it wasn’t).
So I wanted to make a advance door where it would check if a player is in within a said distance from a part, if the player was it would make two parts: One part in the front and one part in the back of the door, and when they are touched It’ll tween the door to a 100 degree angle.
So, like i said I wanted to check so far if everything was in tact so I tested to see if the checking part was correctly (the part where it would check if the player is close to the door). No errors, no warnings, no printings:


local Base = workspace.Part--btw, I wasn't testing it with the actual door base yet, 
--because I was predicting that I would come across with a probelm so i tested it with a part first.
local range = 100
	game.Players.PlayerAdded:Connect(function(Player)
		Player.CharacterAdded:Connect(function(Character)
			for i,v in pairs(game.Players:GetPlayers()) do
				local Distance = Player:DistanceFromCharacter(Base.Position)
				if (Distance <= range) then --checks if its close to it
				print("hi]")
				end
			end
		end)
	end)

I’m confused if i’m using it right, like i said no errors or printing even when I’m really close to the part:


And I’m not using runservice because I’m already using a for loop
The script is a child of the part btw

1 Like

It only runs once, when someone spawns in it cycles through the players and then stops. Make it a while true loop.

2 Likes

Ah, so is something like this ideal?


local Base = workspace.Part
local range = 100
	game.Players.PlayerAdded:Connect(function(Player)
		Player.CharacterAdded:Connect(function(Character)
		for i,v in pairs(game.Players:GetPlayers()) do
			while true do
				local Distance = Player:DistanceFromCharacter(Base.Position)
				if (Distance <= range) then
					print("hi]")
				end
			end
		end
	end)
end)

or just replace the for loop for the while loop? or do i wrap the while loop around the playeradded event?

He’s saying you should do it like this

        Player.CharacterAdded:Connect(function(Character)
			while true do
				local Distance = Player:DistanceFromCharacter(Base.Position)
				if (Distance <= range) then --checks if its close to it
				    print("hi]")
				end
                wait()
			end
		end)

There’s no point of this line

for i,v in pairs(game.Players:GetPlayers()) do
4 Likes

So the end script looks like this and now it works:


game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		while true do
			local Base = script.Parent
			local range = 100
		local Distance = Player:DistanceFromCharacter(Base.Position)
		if (Distance <= range) then --checks if its close to it
			print("hi]")
			end
		wait()
		end
	end)
end)
1 Like

Just a side note, you don’t need to define the Base and range every time you loop through. Also I recommend using Heartbeat instead of wait().

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local Base = script.Parent
local range = 100

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		while true do
			local Distance = Player:DistanceFromCharacter(Base.Position)
			
			if (Distance <= range) then
				print("hi]")
			end
			
			RunService.Heartbeat:Wait()
		end
	end)
end)
2 Likes