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

Finding if a player is within a range or not? Welding help.

I am trying to make handcuffs which will allow Police to arrest prisoners when they are in certain range. For that I made a sphere with radius of range and welded it to the Police Torso. I set cancollide to false. But when I play it , the weld disrupts my players animations and when I jump it pushes me back. Here is the script-

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local range = game.ServerStorage.Range:Clone()
		range.Parent = char
		range.CFrame = char.UpperTorso.CFrame
		local weld = Instance.new("Weld")
		weld.Part0 = char.UpperTorso
		weld.C0 = char.UpperTorso.CFrame
		weld.Part1 = range
		weld.C1 = range.CFrame
		weld.Parent = char.UpperTorso
	end)
end)

For now I am doing it to players added as I wanted to check it out. Any help will be appreciated.

1 comment

0

Edited in regards to your comment

1 answer

✓ Accepted answer

You do not have to do anything with welds to get the distance between to things. You can use the following function instead.

function getDistance(p1,p2)
	--p1 and p2 must both be Vector3's or Vector2's
	return (p1-p2).magnitude
end

Simply supply the positions of two things and it will return the distance between them. Then, using a simple if statement to check if a player is close enough to another, you can acheive your desired effect.

You can read about the magnitude property of Vector3's here.

You can also read more about Vectors in general here and here.

In regards to your comment, yes, you do.

for i, player in pairs(game.Players:GetPlayers()) do
	--check if close enough to do anything
end

2 comments

0

But how do I come to know whether a person is in a certain distance or not. I don't have reference to every player, ingame.

0

Oh ok. thx.

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