How to detect if Client is in radius of one part in descendants instead of all parts in descendants?

I want to detect if the Client (LocalPlayer) is in radius of one part in a folder with GetDescendants instead of all parts in the folder/parts with property I’m looking for. (Using to detect if player is in radius of a machine in round for my game)

image
(Picture above shows what happens when player is radius of only 1 part)

LocalScript in StarterGui:

wait(6) -- Wait For PlayerGui to be loaded

local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local uiService = game:GetService("UserInputService")
local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")

local hb = rService.Heartbeat
local plr = plrs.LocalPlayer
local char = plr.Character
local hrp

local mp = plr.PlayerGui.MachineProgress
local mw = plr.PlayerGui.MachineWarning
local machine_radius = 8
local machinesFolder
local machines
local progressText = plr.PlayerGui.MachineProgress.Frame.TextLabel

local mEvent = rStorage:WaitForChild("Machine",8)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",8)

local pressedE = false

while wait() do
	if game.Workspace:FindFirstChild("Machines", true) then
		mp = plr.PlayerGui.MachineProgress
		mw = plr.PlayerGui.MachineWarning
		machine_radius = 8
		machinesFolder = game.Workspace:FindFirstChild("Machines", true)
		machines = machinesFolder:GetDescendants()


		uiService.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				pressedE = true
			end
		end)


		local function onRecieveMachineData(progress)
			progressText.Text = ""..progress.."%"
			progressText.Parent.ProgressBar:TweenSize(
				progressText.Parent.ProgressBar.Size + UDim2.new(0.01,0,0,0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				0.99,
				true
			)
		end

		mUIEvent.OnClientEvent:Connect(onRecieveMachineData)

		task.spawn(function()
			while wait(.5) do
				for index, machine in pairs(machines) do
					if machine.Name == "Radio" then
						if char:FindFirstChild("HumanoidRootPart") then
							hrp = char:FindFirstChild("HumanoidRootPart")
							if (hrp.Position-machine.Position).magnitude < machine_radius then
								print("in radius")
								mp.Enabled = false
								mw.Enabled = true
								if pressedE then
									mEvent:FireServer(true,machine,plr.Character)
									print("firing to server to start working on machine")
									mp.Enabled = true
									mw.Enabled = false
								end
							else
								pressedE = false
								mw.Enabled = false
								mp.Enabled = false
								mEvent:FireServer(false,machine,plr.Character)
								print("client not in radius")
							end
						end
					end
				end
			end
		end)
	end
end
function inRadius(part , radius , character)
	local distance = (character.HumanoidRootPart.Position - part.Position).Magnitude
	local isInRadius = distance <= radius and true or false
	print(isInRadius)
	return isInRadius
end
1 Like

How do I use what it returns (I’m not too good with functions)

It returns a Boolean value, true if the player’s character is in range and false if otherwise.

1 Like

How do I use it in an if statement??
image

image

wait(6)

local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local uiService = game:GetService("UserInputService")
local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")

local hb = rService.Heartbeat
local plr = plrs.LocalPlayer
local char = plr.Character
local hrp

local mp = plr.PlayerGui.MachineProgress
local mw = plr.PlayerGui.MachineWarning
local machine_radius = 8
local machinesFolder
local machines
local progressText = plr.PlayerGui.MachineProgress.Frame.TextLabel

local mEvent = rStorage:WaitForChild("Machine",8)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",8)

local pressedE = false

while wait() do
	if game.Workspace:FindFirstChild("Machines", true) then
		mp = plr.PlayerGui.MachineProgress
		mw = plr.PlayerGui.MachineWarning
		machine_radius = 8
		machinesFolder = game.Workspace:FindFirstChild("Machines", true)
		machines = machinesFolder:GetDescendants()


		uiService.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				pressedE = true
			end
		end)


		local function onRecieveMachineData(progress)
			progressText.Text = ""..progress.."%"
			progressText.Parent.ProgressBar:TweenSize(
				progressText.Parent.ProgressBar.Size + UDim2.new(0.01,0,0,0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				0.99,
				true
			)
		end

		mUIEvent.OnClientEvent:Connect(onRecieveMachineData)
		
		function inRadius(part , radius , character)
			local distance = (character.HumanoidRootPart.Position - part.Position).Magnitude
			local isInRadius = distance <= radius and true or false
			print(isInRadius)
			return isInRadius
		end

		task.spawn(function()
			while wait(.5) do
				for index, machine in pairs(machines) do
					if machine.Name == "Radio" then
						if char:FindFirstChild("HumanoidRootPart") then
							hrp = char:FindFirstChild("HumanoidRootPart")
							inRadius(machine, machine_radius, char)
							if inRadius == true then
								print("in radius")
								mp.Enabled = false
								mw.Enabled = true
								if pressedE then
									mEvent:FireServer(true,machine,plr.Character)
									print("firing to server to start working on machine")
									mp.Enabled = true
									mw.Enabled = false
								end
							else
								pressedE = false
								mw.Enabled = false
								mp.Enabled = false
								mEvent:FireServer(false,machine,plr.Character)
								print("client not in radius")
							end
						end
					end
				end
			end
		end)
	end
end

Same problem happens detects it is in radius of one machine but also says its not in radius

Try this

                            local isInRadius = inRadius(machine, machine_radius, char)
							if isInRadius == true then
								print("in radius")
								mp.Enabled = false
								mw.Enabled = true
								if pressedE then
									mEvent:FireServer(true,machine,plr.Character)
									print("firing to server to start working on machine")
									mp.Enabled = true
									mw.Enabled = false
								end
							else
								pressedE = false
								mw.Enabled = false
								mp.Enabled = false
								mEvent:FireServer(false,machine,plr.Character)
								print("client not in radius")
							end

Tried this (still doesnt work):

wait(6)

local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local uiService = game:GetService("UserInputService")
local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")

local hb = rService.Heartbeat
local plr = plrs.LocalPlayer
local char = plr.Character
local hrp

local mp = plr.PlayerGui.MachineProgress
local mw = plr.PlayerGui.MachineWarning
local machine_radius = 8
local machinesFolder
local machines
local progressText = plr.PlayerGui.MachineProgress.Frame.TextLabel
local inRadiusBool = false

local mEvent = rStorage:WaitForChild("Machine",8)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",8)

local pressedE = false

while wait() do
	if game.Workspace:FindFirstChild("Machines", true) then
		mp = plr.PlayerGui.MachineProgress
		mw = plr.PlayerGui.MachineWarning
		machine_radius = 8
		machinesFolder = game.Workspace:FindFirstChild("Machines", true)
		machines = machinesFolder:GetDescendants()


		uiService.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				pressedE = true
			end
		end)


		local function onRecieveMachineData(progress)
			progressText.Text = ""..progress.."%"
			progressText.Parent.ProgressBar:TweenSize(
				progressText.Parent.ProgressBar.Size + UDim2.new(0.01,0,0,0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				0.99,
				true
			)
		end

		mUIEvent.OnClientEvent:Connect(onRecieveMachineData)
		
		function inRadius(part , radius , character)
			local distance = (character.HumanoidRootPart.Position - part.Position).Magnitude
			local isInRadius = distance <= radius and true or false
			print(tostring(isInRadius).." from function")
			inRadiusBool = isInRadius
		end

		task.spawn(function()
			while wait(.5) do
				for index, machine in pairs(machines) do
					if machine.Name == "Radio" then
						if char:FindFirstChild("HumanoidRootPart") then
							hrp = char:FindFirstChild("HumanoidRootPart")
							inRadius(machine, machine_radius, char)
							print(tostring(inRadiusBool).." not from funciton")
							if inRadiusBool == true then
								print("in radius")
								mp.Enabled = false
								mw.Enabled = true
								if pressedE then
									mEvent:FireServer(true,machine,plr.Character)
									print("firing to server to start working on machine")
									mp.Enabled = true
									mw.Enabled = false
								end
							else
								pressedE = false
								mw.Enabled = false
								mp.Enabled = false
								mEvent:FireServer(false,machine,plr.Character)
								print("client not in radius")
							end
						end
					end
				end
			end
		end)
	end
end

Screenshot_204

(console when player is actually in radius)

PROBLEM: game detects in radius and not in radius at same time when there are multiple parts to check (if there is one it works)

image
(what console looks like when there is only one part)

Just did (check my latest post)

local IsInRadius = inRadius() --returns true if he is in the radius and false if not. 

That yields the same result (doesn’t solve the problem)

I figured it out now :smile:

wait(6) -- Wait For PlayerGui to be loaded

local ws = game:GetService("Workspace")
local plrs = game:GetService("Players")
local uiService = game:GetService("UserInputService")
local rService = game:GetService("RunService")
local rStorage = game:GetService("ReplicatedStorage")

local hb = rService.Heartbeat
local plr = plrs.LocalPlayer
local char = plr.Character
local hrp

local mp = plr.PlayerGui.MachineProgress
local mw = plr.PlayerGui.MachineWarning
local machine_radius = 8
local machinesFolder
local machines
local progressText = plr.PlayerGui.MachineProgress.Frame.TextLabel
local machineToCheck = nil

local mEvent = rStorage:WaitForChild("Machine",8)
local mUIEvent = rStorage:WaitForChild("ChangeMachineUI",8)

local pressedE = false

while wait() do
	if game.Workspace:FindFirstChild("Machines", true) then
		mp = plr.PlayerGui.MachineProgress
		mw = plr.PlayerGui.MachineWarning
		machine_radius = 8
		machinesFolder = game.Workspace:FindFirstChild("Machines", true)
		machines = machinesFolder:GetDescendants()


		uiService.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				pressedE = true
			end
		end)


		local function onRecieveMachineData(progress)
			progressText.Text = ""..progress.."%"
			progressText.Parent.ProgressBar:TweenSize(
				progressText.Parent.ProgressBar.Size + UDim2.new(0.01,0,0,0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				0.99,
				true
			)
		end

		mUIEvent.OnClientEvent:Connect(onRecieveMachineData)

		for index, machine in pairs(machines) do
			if machine.Name == "Radio" then
				if char:FindFirstChild("HumanoidRootPart") then
					hrp = char:FindFirstChild("HumanoidRootPart")
					if (hrp.Position-machine.Position).magnitude < machine_radius then
						print("in radius")
						machineToCheck = machine
					end
				end
			end
		end
	end

	if machineToCheck ~= nil then
		if (hrp.Position-machineToCheck.Position).magnitude < machine_radius then
			print("in radius")
			mp.Enabled = false
			mw.Enabled = true
			if pressedE then
				mEvent:FireServer(true,machineToCheck,plr.Character)
				print("firing to server to start working on machine")
				mp.Enabled = true
				mw.Enabled = false
			end
		else
			pressedE = false
			mw.Enabled = false
			mp.Enabled = false
			mEvent:FireServer(false,machineToCheck,plr.Character)
			print("client not in radius")
		end
	end
	
end