Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I manipulate descendants in this case?

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.

1 comment

0

You would have to connect a function (that changes the color of the part) to the touched event.

4 answers

✓ Accepted answer

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. ####I hope this helps! Have a great day scripting.


More on...

1 comment

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.

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.

1 comment

0

Sorry for this answer it feels incomplete and I am working on bringing the best possible script for this question. Very Soon!

This discussion is preserved as it was. New answers, comments, and votes are closed.