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

This welding script is being a pain. Apparently "Head" isn't a valid member of workspace. Help?

Asked by 6 years ago
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:GetChildren("Humanoid") then
        script.Parent.Anchored = false
        script.Parent.WeldConstraint.Part1 = hit.Parent.Head.Humanoid
    end
end)

there's the code

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Firstly, GetChildren needs to be replaced with FindFirstChild as GetChildren is used to return a table with all the children in the object. This means it will automatically return a result equivalent to true for the if statement. You need to use FindFirstChild to check if an object exists. If the object doesn't exist, FindFirstChild will return a nil result, a value that is equivalent to false for the if statement.

You also need to create the object WeldConstraint before you can use it. "Instance.new("WeldConstraint", script.Parent)" must be placed between the anchored line and the WieldConstraint.Part1 line. Therefore we might as well use a local reference for the created instance. That way we don't use a different object than the one we created if there is another object called "WieldConstraint".

You also need to set the Part0 property of the WeldConstraint to the base object as well for the WieldConstraint to work correctly. With both Part0 and Part1 properties set the WieldConstraint will become active and both parts will be attached.

Finally Head does not have a humanoid object by default, and a Humanoid object can't actually be used for the Part1 property. You must use the head Part instead to set the Part1 property of the WeldConstraint.

This is my fixed version of the script:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Corrected GetChildren to FindFirstChild
        script.Parent.Anchored = false
    local WieldConstraint = Instance.new("WeldConstraint", script.Parent) -- Create a new WieldConstraint object
        WieldConstraint.Part0 = script.Parent -- So we can attach this properly
        WieldConstraint.Part1 = hit.Parent.Head -- Humanoid cannot be used for this property.
    end
end)

If you've already created a WeldConstraint object, remove the "local WieldConstraint = Instance.new("WeldConstraint", script.Parent)" lien and replace both references to "WieldConstraint." to "script.Parent.WeldConstraint.".

0
Did you assume OP didn't already create it lmao User#19524 175 — 6y
0
Yes. MatthewCenance 35 — 6y
0
I have now changed it to inform the user that "If you've already created a WeldConstraint object, remove the "local WieldConstraint = Instance.new("WeldConstraint", script.Parent)" lien and replace both references to "WieldConstraint." to "script.Parent.WeldConstraint."." MatthewCenance 35 — 6y
0
I did make the weldconstraint, but the thing works regardless. DuckMaster11211 13 — 6y
Ad
Log in to vote
0
Answered by
systack 123
6 years ago
Edited 6 years ago

You're attempting to pass the string "Humanoid" to a function which doesn't take any arguments.

It seems you're confusing the GetChildren function for the function FindFirstChild (or one of its sister functions). As a result, your if statement is always running; hit.Parent:GetChildren() will always evaluate to true (tables are truthy).

So if, for example, the part were to touch the baseplate, then the if statement will evaluate to true and the script will attempt look in it's parent (Workspace) for "Head", which most likely does not exist in the workspace.

By using FindFirstChildOfClass, we should be able to achieve intended behavior

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then
        script.Parent.Anchored = false
        script.Parent.WeldConstraint.Part1 = hit.Parent.Head.Humanoid
    end
end)
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Line 2 won't work how you would expect for it to. GetChildren returns an array of children of the object you called it on, and takes no arguments. Your "Humanoid" argument was discarded, and the if only executed because you used a table as a condition. In Lua, anything that is not false or nil is truthy. A table is not false or nil, and in a boolean context is it considered truthy. The fix would be to use the Instance:FindFirstChild(string name, bool recursive = false) method, and what it does is find the first child with name name. If no child is found, it returns nil. The second recursive argument is a boolean that determines if it should search through descendants as well and not just children. It defaults to false, it won't find a child recursively, and you won't need to worry about including that argument.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        script.Parent.Anchored = false
        script.Parent.WeldConstraint.Part1 = hit.Parent.Head -- you might want to do just hit.Parent.Head...
    end
end)

0
By "truthy" you mean "true" right? MatthewCenance 35 — 6y
0
self explanatory. tables are truthy, not true. User#19524 175 — 6y
0
This is the first time I've heard of "truthy" in Lua scripting. This is why I thought it was an error. MatthewCenance 35 — 6y

Answer this question