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

Why can not get model's humanoid through its name?

I try to make a commanding model, but it doesn't seem to get the model's Humanoid.\

local rp = game:GetService("ReplicatedStorage")
local remote = rp:FindFirstChild("Z")



remote.OnServerEvent:Connect(function(player, val, Mouse)


	if val == "CommandZombie" then
		local Zombiepet = workspace:GetChildren(player.Name.."'s Pet")
		local zomNoid = Zombiepet:FindFirstChild("HumanoidRootPart")
		print(("ZOmbieeeeeee"))
		if Zombiepet and zomNoid then
			print(("ZOmbie"))
			local bodyposition = Instance.new("BodyPosition")
			bodyposition.Parent = zomNoid 
			bodyposition.MaxForce = Vector3.new(10000,10000,10000)
			Zombiepet.Position = Mouse
			game.Debris:AddItem(bodyposition, 4)
		end

	end
end)



1 comment

0

GetChildren() doesn't have parameters and returns a table. You can give it anything you want and it won't matter, because there are no parameters to give arguments to; GetChildren() will still give you a table of every child under Workspace. Either use FindFirstChild()/WaitForChild() or loop through the existing table.

1 answer

✓ Accepted answer

On line 10, workspace function GetChildren (https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren) returns an array of children parented under workspace. In this case, the arguments wouldn't have any change on the returned values. (Note: this applies to some functions.)

I suggest changing GetChildren to FindFirstChild.

Sample script proving that the arguments of GetChildren have no influence on the returned values.

local workspace = workspace

local t1 = workspace:GetChildren()
local t2 = workspace:GetChildren('wow potato')

for a=1,#t1 do
	print(t1[a]==t2[a]) -- true
end

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