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

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

Asked by 4 years ago
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.

0
Mark XTOOTHLESX's answer as accepted SteamG00B 1633 — 4y

3 answers

Log in to vote
4
Answered by
Axenori 124
4 years ago

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!

0
Thanks for the explanation I forgot about i , v loop. RebornedSnoop 175 — 4y
0
Oops sorry I forgot to accept the answer. My apologies RebornedSnoop 175 — 4y
Ad
Log in to vote
1
Answered by
bnxDJ 57
4 years ago

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).

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago

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)

Answer this question