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