Can variables store multiple values?

local bodyGyro = script.Parent.BodyGyro
local plrs = game:GetService("Players")
local players = plrs:GetPlayers()
local part = script.Parent
local space = 30

function findClosestPlayer(position)
	local root = nil
	local h = nil
	local fake = nil
	local children = game.Workspace:GetChildren()
	local space = 30
	for i, v in pairs(children) do
		local stuff = children[i]
		if v:IsA("Model") then
			local h = stuff:FindFirstChild("Humanoid")
			if h then
				root = stuff:FindFirstChild("HumanoidRootPart")
				if root then
					if (root ~= nil) and (h ~= nil) and (h.Health > 0) then
						if (root.Position - position).magnitude < space then
							print("work")
							fake = root
						end
					end
				end
			end		
		end
	end
	return fake
end

while wait() do
	local t = findClosestPlayer(part.Position)
	if t ~= nil then
		bodyGyro.CFrame = CFrame.new(part.Position, t.Position)
	end
end

I just wanna make sure that
local stuff = children[i]
then local h = stuff:FindFirstChild(“Humanoid”)
does h = all the children that are humanoids, as it iterates through each one, so would it be stored like
h = humanoid1, humanoid2
based on how many there are??
sorry im not good at asking questions this might or might not seem confusing

1 Like

use local h = stuff:GetChildren to make it a list
then use a loop:

for i, v in pairs(h) do
   whatever you want to do with each one
end
2 Likes

Why are you doing this? v is already children[i], you don’t need to use a separate variable. Also, this is a terrible method since you could just loop through all players and check if their characters are close rather than checking every single part in the workspace.

2 Likes

it says findfirstchild, does it mean it finds just the first humanoid, or since it iterates through each object in workspace would it find every object and check if they’re humanoid? im so lost lol

The loop runs separately for each member of the workspace. Each time it repeats, it finds a new child and checks that child for a humanoid. h will be a different humanoid at different iterations of the loop, but it will never be multiple humanoids at once.

6 Likes