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
6 years ago
1local descendants = script.Parent:GetDescendants()
2 
3for index, descendant in pairs(descendants) do
4    if descendant.Name == "Grass" and descendant.Touched == true then
5        descendant.BrickColor = BrickColor.new("Bright green")
6    end
7end

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 — 6y

4 answers

Log in to vote
3
Answered by
Rheines 661 Moderation Voter
6 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.

01local descendants = script.Parent:GetDescendants()
02 
03for index, descendant in pairs(descendants) do
04    if descendant.Name == "Grass" then
05        --Touched event connected to a function that changes the brick color.
06        descendant.Touched:Connect(function(hit)
07            descendant.BrickColor = BrickColor.new("Bright green")
08        end)
09    end
10end

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.

01local descendants = script.Parent:GetDescendants()
02 
03for index, descendant in pairs(descendants) do
04    if descendant.Name == "Grass" then
05        --Touched event connected to a function that changes the brick color.
06        descendant.Touched:Connect(function(hit)
07            --Check whether or not a player is touching the grass.
08            if hit.Parent:FindFirstChild("Humanoid") then
09                descendant.BrickColor = BrickColor.new("Bright green")
10            end
11        end)
12    end
13end
Ad
Log in to vote
4
Answered by 6 years ago
Edited 6 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:

01local descendants = script.Parent:GetDescendants()
02 
03for _, descendant in pairs(descendants) do
04    --[[
05        we aren't using index, so let's
06        just replace it with "_" a Lua convention
07        for naming things we don't use.
08    --]]
09    if descendant.Name == "Grass" then
10        local connection
11        connection = descendant.Touched:Connect(function(hit)
12            if hit.Parent:FindFirstChild("Humanoid") then -- making sure it's a player touching
13                descendant.BrickColor = BrickColor.new("Bright green")
14                --[[
15                    Unless you wish otherwise, I don't see
View all 25 lines...

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 — 6y
Log in to vote
2
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 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.

01local descendants = script.Parent:GetDescendants()
02 
03for index, descendant in pairs(descendants) do
04    if descendant.Name == "Grass" then
05        --Connect function to Touched event, 'h' is object that hit
06        descendant.Touched:Connect(function(h)
07            --Make sure it was a player
08            local plr = h.Parent:FindFirstChild("Humanoid")
09            if plr then
10                --Configure BrickColor
11                descendant.BrickColor = BrickColor.new("Bright green")
12            end
13        end)
14    end
15end

Happy developing!

Log in to vote
-3
Answered by 6 years ago

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

01--I'd advise the script to be in ServerScriptStorage but here it is as normal:
02 
03tChildren = script.Parent:GetDescendants()
04 
05function COLOR(ColorSelect)
06for _,z in pairs (tChildren) do
07if z:IsA("BasePart") and z.BrickColor ~= ColorSelect then z.BrickColor = BrickColor.new(ColorSelect)
08end end
09 
10script.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 — 6y

Answer this question