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

How do I manipulate descendants in this case?

Asked by
Dwayder 29
5 years ago
local descendants = script.Parent:GetDescendants()

for index, descendant in pairs(descendants) do
    if descendant.Name == "Grass" and descendant.Touched == true then
        descendant.BrickColor = BrickColor.new("Bright green")
    end
end

I wanna make all of the descendants of a model the color "Bright green" when touched by the player. I am probably using .Touched wrong but this was the only way I thought of and yeah... It's not working.

0
You would have to connect a function (that changes the color of the part) to the touched event. User#25115 0 — 5y

4 answers

Log in to vote
3
Answered by
Rheines 661 Moderation Voter
5 years ago

As mentioned already, you need to connect a function to each grass via a .Touched event. The way you syntax the .Touched event currently is wrong.

local descendants = script.Parent:GetDescendants()

for index, descendant in pairs(descendants) do
    if descendant.Name == "Grass" then
        --Touched event connected to a function that changes the brick color.
        descendant.Touched:Connect(function(hit)
            descendant.BrickColor = BrickColor.new("Bright green")
        end)
    end
end

If grass is touched by any part, it will turn green. If you wish only the player is able to trigger the color change, you can find a humanoid in the part's parent.

local descendants = script.Parent:GetDescendants()

for index, descendant in pairs(descendants) do
    if descendant.Name == "Grass" then
        --Touched event connected to a function that changes the brick color.
        descendant.Touched:Connect(function(hit)
            --Check whether or not a player is touching the grass.
            if hit.Parent:FindFirstChild("Humanoid") then
                descendant.BrickColor = BrickColor.new("Bright green")
            end
        end)
    end
end
Ad
Log in to vote
4
Answered by 5 years ago
Edited 5 years ago

You have to connect a function to the touched event that changes the color (for each descendant that is named "Grass").


Here is an example:

local descendants = script.Parent:GetDescendants()

for _, descendant in pairs(descendants) do 
    --[[
        we aren't using index, so let's 
        just replace it with "_" a Lua convention
        for naming things we don't use.
    --]]
    if descendant.Name == "Grass" then
        local connection
        connection = descendant.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then -- making sure it's a player touching
                descendant.BrickColor = BrickColor.new("Bright green")
                --[[
                    Unless you wish otherwise, I don't see
                    why you would want this touched event
                    to fire other than the first time the player 
                    touches the grass, so I'll disconnect the event
                    once it is fired once.
                --]]
                connection:Disconnect()
            end
        end)
    end
end

We can't use descendant.Touched:Wait() because that would yield the script until the event happened, and the loop would not finish.

I hope this helps! Have a great day scripting.


More on...

0
In the last link I provided, a lot of the script examples use "if hit.Parent:FindFirstChild("Humanoid") ~= nil then" This is redundant. "nil" is falsey, so an if statement will treat it as false. User#25115 0 — 5y
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
5 years ago

Touched is an event, not a property. Events use the Connect function to establish another function to be called whenever the event is fired.

You need to use the Connect function to make a Touched event for descendant. Note that you must check if the object that hit was a player. This can be done by checking for a Humanoid, since all Character's have this object.

local descendants = script.Parent:GetDescendants()

for index, descendant in pairs(descendants) do
    if descendant.Name == "Grass" then
        --Connect function to Touched event, 'h' is object that hit
        descendant.Touched:Connect(function(h)
            --Make sure it was a player
            local plr = h.Parent:FindFirstChild("Humanoid")
            if plr then
                --Configure BrickColor
                descendant.BrickColor = BrickColor.new("Bright green")
            end
        end)
    end
end

Happy developing!

Log in to vote
-3
Answered by 5 years ago

Righty. Sorry to slap you with a script right off the bat:

--I'd advise the script to be in ServerScriptStorage but here it is as normal:

tChildren = script.Parent:GetDescendants()

function COLOR(ColorSelect)
for _,z in pairs (tChildren) do
if z:IsA("BasePart") and z.BrickColor ~= ColorSelect then z.BrickColor = BrickColor.new(ColorSelect)
end end

script.Parent.Touched:Connect(COLOR("Bright green")

This is going to do the following:

A) Lookup children A.K.A descendants

B) Use a Touched event to turn them Bright Green and wala!

If you have something to say don't be shy, we're a team now.

0
Sorry for this answer it feels incomplete and I am working on bringing the best possible script for this question. Very Soon! Jan14th2019 -53 — 5y

Answer this question