I’m trying to make it so that whenever you click the Button the part’s transparency, Reflectance and it’s color changes using Click Detector but the problem is that whenever I press the Button it says on the output that it is not a valid member of the Workspace .-.
Here’s the Picture of the Explorer:-
Here’s the Code:-
local prt = script.Parent
function OnClick(hit)
local Hum = hit.Parent:findFirstChild("Humanoid")
if Hum ~= nil then
prt.Transparency = 0.5
prt.Reflectance = 0
prt.BrickColor = BrickColor.new("White")
end
end
prt.ClickDetector.MouseClick:Connect(OnClick)
local prt = script.Parent
function OnClick()
prt.Transparency = 0.5
prt.Reflectance = 0
prt.BrickColor = BrickColor.new("White")
end
prt:WaitForChild("ClickDetector").MouseClick:Connect(OnClick)
-- wait for it to load into the game
When the server starts, objects have different points at which they load, server scripts loads into the game before certain objects. Therefore, you have to use WaitForChild. In other words, it pauses the script until the object exists so the script doesn’t throw any x is not a valid member of y 'z' errors.
@HugeCoolboy2007 So basically, Roblox does not guarantee the time or order in which objects are replicated from the server to the client so we use WaitForChild for this purpose.