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

The children in workspace won't update when I do workspace:GetChildren()?

Before I press "Play", there are 3 things in workspace, Camera, Terrain, Baseplate. When I click play, there are 4 things because my character loads in. In the script below, after adding parts into the workspace after pressing "Play", when I click on the GUI button it prints "its cycling through workspace" 4 times. If I have an extra part in the workspace after clicking "Play", shouldn't it say that 5 times?

My problem is, I need the children to update whenever something is added into workspace during gameplay.

script.Parent.MouseButton1Click:Connect(function()
	
	w = workspace:GetChildren()
	
	for i,v in pairs(w) do
		print("its cycling through workspace")
		if v.Name == "Spray" then
			print("The if statement works")
			v:Destroy()
		end
	end 
	
end)

2 comments

1

Um. W is the workspace but then its not going to use instance:WaitForchild() . Which means its just looking and seeing the workspace without actually doing anything.

0

Try defining "w" as a local variable. I don't see why this should work but it's in the realm of possibility.

1 answer

Try this tell me if it doesn't work

script.Parent.MouseButton1Click:Connect(function()
	
for i,v in pairs(workspace:GetChildren()) do
	
print("its cycling through workspace")

if v.Name == "Spray" then
	
print("The if statement works")

v:Destroy()

end

end

end)

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