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

Setting anchored to false not working in an local script how to fix?

script.Parent.MouseButton1Click:Connect(function()
	local Child = game.Workspace.GGG:GetChildren()
	Child.Anchored = false
end)

I just watched a tutorial and wanted to try something new with :GetChildren(). This script is to set the two parts anchored property , inside a model "GGG" to false. This code is inside an local script. Please help.

1 comment

0

Mark XTOOTHLESX's answer as accepted

3 answers

When using :GetChildren() it returns a table, you can iterate through it using a i, v in pairs loop. You also have some other errors

Here is how your script should look:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
local GC = game.Workspace.GGG:GetChildren()
for i, v in pairs(GC) do
v.Anchored = true
end
end)

Hope this helps!

2 comments

0

Thanks for the explanation I forgot about i , v loop.

0

Oops sorry I forgot to accept the answer. My apologies

You are trying to change a property of an array. (list of parts you got when getting all the children inside the model)

you would have to do it like this.

script.Parent.MouseButton1Click:Connect(function()
    local Child = game.Workspace.GGG:GetChildren()
	for i = 1,#Child do -- for i = starting number,Number of things in array do
    	Child[i].Anchored = false
	end
end)

This goes through all the children and changes their .Anchored to false. In case you didn't know this way the parts would only be unanchored for the player pressing the button and not for other players because it's a local script (local as in on their own game).

Mark XTOOTHLESX's answer because he got it correct first, but I am also making this just to show a different way of iterating through tables that I find to be easier to remember and easier to work with.

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	local GC = game.Workspace.GGG:GetChildren()
	table.foreach(GC, function(i,v)
		v.Anchored = true
	end)
end)

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