Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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)



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. DeceptiveCaster 3761 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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
Ad

Answer this question