1 | local descendants = script.Parent:GetDescendants() |
2 |
3 | for 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 |
7 | 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.
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.
01 | local descendants = script.Parent:GetDescendants() |
02 |
03 | for 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 |
10 | 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.
01 | local descendants = script.Parent:GetDescendants() |
02 |
03 | for 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 |
13 | end |
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:
01 | local descendants = script.Parent:GetDescendants() |
02 |
03 | for _, 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 |
We can't use descendant.Touched:Wait()
because that would yield the script until the event happened, and the loop would not finish.
More on...
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.
01 | local descendants = script.Parent:GetDescendants() |
02 |
03 | for 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 |
15 | end |
Happy developing!
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 |
03 | tChildren = script.Parent:GetDescendants() |
04 |
05 | function COLOR(ColorSelect) |
06 | for _,z in pairs (tChildren) do |
07 | if z:IsA( "BasePart" ) and z.BrickColor ~ = ColorSelect then z.BrickColor = BrickColor.new(ColorSelect) |
08 | end end |
09 |
10 | 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.