Help with ":WaitForChild()"

Hello, Devfourm. I am trying to make a game with you that has the ability to resize, colour, materialize, and add effects to a basic part. I have recently added something where you can change the properties of the Sparkles if you have them added to the part. Once these Sparkles are added by using the button in the video, you can change the colour. But when I use the “Rest Part” Button, (which uses the :ClearAllChildern().) the buttons to change the sparkle colour don’t work anymore. Here is the script for the Orange button (They are all the same just different names):

local Sparkles = game.Workspace:WaitForChild("Color_Part"):WaitForChild("Sparkles")
local Click = script.Parent.ClickDetector
local Text = script.Parent.Parent.Parent.SurfaceGui.TextLabel

Click.MouseClick:Connect(function()
	Sparkles.SparkleColor = Color3.new(1, 0.666667, 0)
	Text.Text = "Orange"
end)
 

The script uses the :WaitForChild() to look for the “Sparkles” as a child of the Color_Part (Witch is the main part).

Here is the video:
https://drive.google.com/file/d/1jiAIjolNEePcsJb2GNWfsSAxtMioQejJ/view?usp=sharing

Thank you :slight_smile:

Instead of only accessing the sparkle thing at the start of the script, you may wish to access it every time the mouse is clicked. This means that even if it is reset, then the script will detect the new one instead.

local ColorPart = game.Workspace:WaitForChild("Color_Part")
local Click = script.Parent.ClickDetector
local Text = script.Parent.Parent.Parent.SurfaceGui.TextLabel

Click.MouseClick:Connect(function()
	ColorPart:WaitForChild("Sparkles").SparkleColor = Color3.new(1, 0.666667, 0)
	Text.Text = "Orange"
end)
 
1 Like

Basically your script isn’t working because your “Sparkles” variable is set to the old sparkles so when you create a new one the script doesn’t know what happened to the old “Sparkles” variable (now nil). Your new sparkles has to be set again each time for the script to know what “Sparkles” you are indexing (adjusting)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.