How do i find a part that hasn't spawned yet?

ok so i have made a tool that uses instance to spawn a part and despawns it with no problem but i want to make the part that spawns do something but first i need to find it in my workspace but i don’t know how to do that if it hasn’t spawned yet i tried WaitForChild but it gives out an infinite yield so i want to know how i can find and use the part to make it do stuff like give out a particle or change the value of cash of the player the only thing i want to know is how can i make the part into a variable and use it.

thanks for any help.

Edit: i found out how to do it myself hope it helps you too.

3 Likes

Assuming you have direct access to the part spawned, you can use the DescendantAdded event.

workspace.DescendantAdded:Connect(function(descendant) 

    if part == descendant then
        — the part was added, run this code.
    end

end)

There’s also the Ancestry changed events you could use. There are a few ways you can go about doing this.

2 Likes

dose that mean the parts name or?

No, that means the actual part instance.

1 Like

sorry to ask but can you give me a full example? if not i understand why not.

Somewhere in some random script.

local part = Instance.new("Part")
part.Name == "SpecialPart"

wait(math.random(6,8)) — wait a random time
part.Parent = workspace — add part to workspace

In a different script detecting when the part was added.

workspace.DescendantAdded:Connect(function(descendant) 

    if descendant.Name == "SpecialPart" then — the name of the part I created in a different script
        — run the code, the part was added
    end

end)
1 Like

game.workspace.DescendantAdded:Connect(function(descendant)

if descendant.Name == "rad2man" then 
	print("i work")
			
end

end)

hey man is there any resion this wount work?
i put it in server script service should i put it somewhere else?

Use the workspace global, but the problem with your code is that, “workspace” is supposed to be capitalized.

You can use game.Workspace, but I recommend you use the global.

1 Like

game.Workspace.part
Okay to use but won’t work if your workspace is named something different.
workspace.part
workspace will always be a valid instance.

2 Likes

So you would do

workspace.DescendantAdded:Connect(function(descendant)
if descendant.Name == "rad2man" then 
	print("i work")
			
end
end)
1 Like

where should i put this script?

I would put it in ServerScriptService.

1 Like

did you mean server script service?

Yes, didn’t mean to put storage.

1 Like

it didnt work, im using a tool to add the part do you think that is the problem?

Could you send a picture of your workspace.

1 Like

image
here it is the part gets added from a script in a tool

And the script is in serverscriptservice?

1 Like

no its in the players backpack when they spawn

Make it a serverscript, and put it in serverstorage.

1 Like