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

Anchored is not a valid member of Model?

I am trying to anchor an object, and then move it. This code works, on one world/place, but when I copied it over to the other, main world/place it fails to unanchor the model. It comes back with this error message: (please mind that everything was copied correctly, the only difference between the two is that I made "ElevCar" into a model instead of a part. -Is that the problem?)

13:03:39.077 - Anchored is not a valid member of Model 13:03:39.078 - Script 'Workspace.Lift.Button.Script', Line 5 13:03:39.078 - Stack End

But, I am trying to unanchor the model "ElevCar", not find a part called "anchored" inside of it.

function onClicked()
	if script.Parent.mot.Value == false then
		script.Parent.mot.Value = true
		if script.Parent.dir.Value == false then
			**script.Parent.Parent.Model.ElevCar.Anchored = false**
			wait()
			script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, 3, 0)
		else
			script.Parent.Parent.Model.ElevCar.Anchored = false
			wait()
			script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, -3, 0)
		end
	end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

Here is the example on the wiki

local part = Instance.new("Part")
part.Anchored = true -- prevent it from falling
part.Parent = Workspace

Maybe you can tell me what I am doing wrong...

1 comment

0

Is that the same with velocity?

3 answers

Anchored

Anchored is a member of BasePart that stops a Part from interacting with physics.

A common mistake

Models do not have the Anchored property. As a result, you can't 'unanchor' a Model. You can however iterate through all of its children and unanchor all of them.

Getting the descendants

Roblox doesn't have a built-in way to get descendants, so here's a shameless plug of the one in Valkyrie, modified to only collect BaseParts.

local function GetDescendants(i)
    local queue = {};
    local returns = {};
    local v = i;
    while v do
        local t = v:GetChildren();
        for i = 1, #t do
            local v = t[i]
		if v:IsA("BasePart") then
            		returns[#returns+1] = v;
		end;
            queue[#queue+1] = v;
        end;
        v = queue[#queue];
        queue[#queue] = nil;
    end;
    return returns;
end;

Iterating

To go through all of the descendants, we'll be using pairs and a for loop, allowing you to iterate over the content of a table in a loop.

Putting it together.

local function GetDescendants(i)
    local queue = {};
    local returns = {};
    local v = i;
    while v do
        local t = v:GetChildren();
        for i = 1, #t do
            local v = t[i]
            returns[#returns+1] = v;
            queue[#queue+1] = v;
        end;
        v = queue[#queue];
        queue[#queue] = nil;
    end;
    return returns;
end;

function onClicked()
    if script.Parent.mot.Value == false then
        script.Parent.mot.Value = true
        if script.Parent.dir.Value == false then
            for k,v in pairs(GetDescendants(script.Parent.Parent.Model.ElevCar) do
		v.Anchored = false
	    end
            wait()
            script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, 3, 0)
        else
            for k,v in pairs(GetDescendants(script.Parent.Parent.Model.ElevCar) do
		v.Anchored = false
	    end
            wait()
            script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, -3, 0)
        end
    end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

If I remember correctly, it is not possible to anchor a model, which is what Anchored is not a valid member of Model 13:03:39.078 is saying. In a simplified explanation, members are the properties of an item. So, if you want to change a light's "properties" the script would read it as members. In the Wiki example you have posted, they are anchoring a part, not a model.

My best guess for fixing this problem is

local anchorme = script.Parent.Parent.Model.ElevCar:GetChildren()--finds the children in your model
if anchorme.ClassName == 'Part' -- if it is a part then
then anchorme.Anchored = false -- false = unanchor, true = anchor.
end

This is my guess to the script.

1 comment

0

Iterate through the table returned by GetChildren. As of now, you're just anchoring a Lua table.

You can use [your model's name] :FindFirstChild("[make a RootPart (any part in the model) and put it's name here]").anchored = false

Here is your code:

function onClick
	if script.Parent.mot.Value == false then
		script.Parent.mot.Value = true
		if script.Parent.dir.Value == false then
			script.Parent.Parent.Model.ElevCar.Anchored = false
			wait()
			script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, 3, 0)
		else
 			script.Parent.Parent.Model.ElevCar.Anchored = false
			wait()
			script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, -3, 0)
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

here is a fixed-up version. Compare them if needed.

function onClick
	if script.Parent.mot.Value == false then
		script.Parent.mot.Value = true
		if script.Parent.dir.Value == false then
			--[[your model's name]] :FindFirstChild("--[[make a RootPart (any part in the model) and put it's name here]]").anchored = false
			wait()
			--[[your model's name]] :FindFirstChild("--[[make a RootPart (any part in the model) and put it's name here]]".BodyVelocity.velocity = Vector3.new(0, 3, 0)
		else
 			script.Parent.Parent.Model.ElevCar.Anchored = false
			wait()
			script.Parent.Parent.Model.ElevCar.BodyVelocity.velocity = Vector3.new(0, -3, 0)
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

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