Detect the player distance between part

I am trying to make a script that detects the distance between the player and the script’s parent and print it.
here is my code:

game.Players.PlayerAdded:Connect(function(Player)
	local MagnitudeInStuds = (script.Parent.Position - Player.Character.HumanoidRootPart.Position).Magnitude 
	print(MagnitudeInStuds)
end)

It gives me this

you need to wait for the player character to spawn in

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait() --add this
	local MagnitudeInStuds = (script.Parent.Position - Player.Character.HumanoidRootPart.Position).Magnitude 
	print(MagnitudeInStuds)
end)
1 Like

Alternatively you could use Player:DistanceFromCharacter(point)

1 Like

How do i make something like if the player is 10 studs away from a part it does a function

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Wait() --add this
	if Player:DistanceFromCharacter(script.Parent.Position) >= 10 then
		-- do stuff
	end
end)

I’ve already tried this but it failed

this probably lags but its probably what youre looking for

game:GetService("RunService").Heartbeat:Connect(function()
	for i,v in ipairs(game.Workspace:GetPartBoundsInRadius(script.Parent.Position,10)) do
		if game.Players:GetPlayerFromCharacter(v.Parent) then
			print("hi") --do thing
		end
	end
end)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.