Script only runs first child when I call GetChildren

Yeha, the title basically says it all. When I run this code it only gets “player1” during 2 player test, but not “Player 2”. This script is supposed to scan players in the players section of explorer, get their character and if the character is close enough it adds a proximity prompt in the humanoid root part.

In this case, it’s only checking player 1, which is the player that’s holding the brick (This script is for handcuffs btw)

here’s the script (Local)



local Equipped = nil
local Tool = script.Parent.Parent

Tool.Equipped:Connect(function(mouse)
	print("A tool was equipped")
	Equipped = true
end)


Tool.Unequipped:Connect(function()
	print("The tool was unequipped")
	Equipped = false
end)


local RunService = game:GetService("RunService")

local RATE_PER_SECOND = 20

RunService.RenderStepped:Connect(function(step)
local increment = RATE_PER_SECOND * step


if Equipped == true then
print("Run")

		-- Generic for-loop example
		local children = game.Players:GetChildren()
		for i, playerInPlayers in ipairs(children) do
			print(playerInPlayers.Name .. " is child number " .. i)
		if playerInPlayers.ClassName ~= "Player" then print("Bruh") return end
			

			if playerInPlayers.Name == Tool.Parent.Name then return end
			
			local char = nil
			local HRP = nil
			
			if playerInPlayers.Character then
					char = playerInPlayers.Character
					HRP = char.HumanoidRootPart
			else	
					return
			end



			local magnitude = (script.Parent.Position - HRP.Position).Magnitude
			print(magnitude)
			if magnitude <= 10 then
				
				print("Close enough")
						local prompt = Instance.new("ProximityPrompt")
						prompt.Parent = HRP
						prompt.Enabled = true
						prompt.HoldDuration = 4
						prompt.Name = "HoldToarrest"
			elseif magnitude > 10 then
				print("Far")
						if not HRP:FindFirstChild("HoldToarrest") then
print("No Prompt")
						else
							HRP.HoldToarrest:Destroy()
						end
					end

				end
					
		
	end
end)

At first I figured it was maybe something to do with renderstepped and maybe thought that the increment was too small, but it didn’t seem to fix anything when I made it higher.

I am confused - what are you using RATE_PER_SECOND and increment for?
Also, in your testing, what values are you printing to the console? I suspect that changing Tool.Parent.Name to Tool.Parent.Parent.Name will correct your issue. When tools are equipped, they are placed in a Player’s Backpack - a subfolder of the Player.

Don’t use return inside of a loop, it will stop the loop. You will need to make the if statement encompass everything even though it looks worse.

1 Like