My player distance detector is not working

So I made a player character distance detector and its working but not working, basically it supposed to show the ui when near a player and it did but its always only showing that 1 random player and not all of them. I remake the whole code with 3 different way and 3 works the same.

Here is my code

while wait() do

if game.ReplicatedStorage.Others.CurrentState.Value == "Start" then
	
	for i, v in pairs(game.Workspace:GetChildren()) do
		
		if v:IsA("Model") and v.Name ~= game.Players.LocalPlayer.Name then
			
			local currentDistance = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
			
			if currentDistance <= 7 then
				
				if v:FindFirstChild("CurrentNumber") then
					
					currentSkillTarget = v
					cloneTarget2 = true
					
					if swapSkill == true then
						
						script.Parent.GameFrame.ActionText.Visible = true
						currentSwapTarget = v
						cloneTarget = true
						
					end
					
				else
					
					currentSkillTarget = v
					
					if swapSkill == true then
						
						
						script.Parent.GameFrame.ActionText.Visible = true
						currentSwapTarget = v
						
					end
					
				end
				
			end
			
			if currentDistance >= 7 then
				
				
				script.Parent.GameFrame.ActionText.Visible = false
				currentSwapTarget = ""
				currentSkillTarget = ""

				cloneTarget = false
				cloneTarget2 = false

			end
			
		end

	end
	
end

end

1 Like

Try using the following function:

local function getPlayersNearPlayer(p: Player, distance: number): {Player}
	local c1 = p.Character
	if not c1 then return {} end 
	local p1 = c1:GetPivot().Position 
	local results = {}
	for _, v in pairs(game.Players:GetPlayers()) do
		if v == p then continue end
		local c2 = v.Character 
		if not c2 then continue end
		local p2 = c2:GetPivot().Position 
		if (p2-p1).Magnitude > distance then continue end
		table.insert(results, v)
	end
	return results 
end

local maxDistance = 100
local player = game.Players.LocalPlayer
print(getPlayersNearPlayer(player, maxDistance))
2 Likes

Dang it works like a charm, btw can you explain to me why mine doesnt work? Last time i also used the table method but it only randomly choose 1 player to make the ui visible. Here is my table method code:

getRunService.Heartbeat:Connect(function()

if game.ReplicatedStorage.Others.CurrentState.Value == "Start" then
	
	for i, v in pairs(game.Players:GetPlayers()) do

		if v.Name ~= game.Players.LocalPlayer.Name then

			local currentDistance = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude

			if currentDistance <= 7 then

				local isInside = false
				for i2, v2 in pairs(currentNearbyChar.currentPlrs) do

					if v2 == v then

						isInside = true

					end

				end

				if isInside == false then

					table.insert(currentNearbyChar.currentPlrs, v)
				
				end

			else

				local deleteSlot = table.find(currentNearbyChar.currentPlrs, v)
				table.remove(currentNearbyChar.currentPlrs, deleteSlot)
				
			end

		end

	end

	for i, v in pairs(game.Workspace:GetChildren()) do

		if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:FindFirstChild("CurrentNumber") then

			if game.Players:FindFirstChild(v.Name) then

			else

				local waitRoot = v:WaitForChild("HumanoidRootPart")
				local currentDistance = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude

				if currentDistance <= 7 then

					local isInside = false
					for i2, v2 in pairs(currentNearbyChar.currentClones) do

						if v2 == v.Name then

							isInside = true

						end

					end

					if isInside == false then

						table.insert(currentNearbyChar.currentClones, v.Name)

					end

				else

					local deletedSlot = table.find(currentNearbyChar.currentClones, v.Name)
					table.remove(currentNearbyChar.currentClones, deletedSlot)

				end

			end

		end

	end
	
end

end)

And my other code here:

while wait() do

if #currentNearbyChar.currentClones == 1 then

	if swapSkill == true then

		script.Parent.GameFrame.ActionText.Visible = true
		for i, v in pairs(currentNearbyChar.currentClones) do

			currentSwapTarget = v
			cloneTarget = true

		end

	end

	for i, v in pairs(currentNearbyChar.currentClones) do

		currentSkillTarget = v
		cloneTarget2 = true

	end

end

if #currentNearbyChar.currentPlrs == 1 then
	
	if swapSkill == true then
		
		script.Parent.GameFrame.ActionText.Visible = true
		for i, v in pairs(currentNearbyChar.currentPlrs) do
			
			currentSwapTarget = v
		
		end
		
	end
	
	for i, v in pairs(currentNearbyChar.currentPlrs) do

		currentSkillTarget = v
	
	end
	
elseif #currentNearbyChar.currentPlrs >= 2 then
	
	local chooseRandom = math.random(1, #currentNearbyChar.currentPlrs)
	if swapSkill == true then
		
		script.Parent.GameFrame.ActionText.Visible = true
		currentSwapTarget = currentNearbyChar.currentPlrs[chooseRandom]
		
	end
	
	currentSkillTarget = currentNearbyChar.currentPlrs[chooseRandom]
	
end

if #currentNearbyChar.currentClones == 0 and #currentNearbyChar.currentPlrs == 0 then

	script.Parent.GameFrame.ActionText.Visible = false
	currentSwapTarget = ""
	currentSkillTarget = ""

	cloneTarget = false
	cloneTarget2 = false
	
end

end

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