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

How to check if something has been parented?

I want to know how I could check if something has been parented. Kinda like a change event when something is parented inside an object the game will notice it, and you'll also have access to the properties of that object. That what I'm trying to achieve but of course there is no way you can use a changed event on only Bool, Int, and String values. Thanks for all the help.

Regards, Bl_ueHistory

2 comments

0

Does the part already have a parent?

0

You could set a variable equal to the current parent and use a while loop to check if it's changed?

4 answers

✓ Accepted answer

If i understood you correctly, this shall help you... i guess...

local object = workspace.Model
object.ChildAdded:Connect(function(part) --runs code if a child is added
	part.CanCollide = true
	part.BrickColor = BrickColor.New("Bright Yellow")
	part.Anchored = true
	--ect
end)

i hope that helped edit: i did not understand the question right, correct? anyways, heres what i think you want to mean:

local object = workspace.Object
object:GetPropertyChangedSignal("Parent"):Connect(function() --runs code if parent changes
	local par = object.Parent--object's parent
	par.CanCollide = true
	par.BrickColor = BrickColor.New("Bright Yellow")
	par.Anchored = true
	--ect
end)

if its not that what you mean EITHER, then explain it better please

Hey, there are 4 ways to do this.

EDIT: I found out about "AncestryChanged" which is the perfect solution (Thanks Aleph)

The first return of the event is the instance that had its parent changed, so in this case 'object' and the second one is the new parent.

object.AncestryChanged:Connect(function(_, newParent)
	print("Parent has been changed")
end)

--

First would be to use GetPropertyChangedSignal("Parent")

local p = Instance.new('Part')
p:GetPropertyChangedSignal('Parent'):Connect(function() 
	print("The parent has been changed") 
end)

or .Changed and check if the property is "Parent"

p.Changed:Connect(function(property) 
	if property == "Parent" then
		print("parent changed")
	end
end)

You can also use .ChildRemoved on the object's parent and check if the object that got removed is the one you know.

local obj = workspace.Object

workspace.ChildRemoved:Connect(function(object)
	if object == obj then
		print("parent has been changed")
	end
end)

In order to check when a property is changed, such as something changing parents, you can use the GetPropertyChangedSignal method in order to get an event that fires when that property is changed.

Example

Here's an example of what you're trying to achieve. Here's a demonstration of the script.

local Part = workspace.Part

local function onParentChanged()
	print("Part's parent is now "..Part.Parent.Name)
end

Part:GetPropertyChangedSignal("Parent"):Connect(onParentChanged) --will print the part's parent's name every time it is changed

An easy way of checking if something has been parented is AncestryChanged. Here's an example:

local Part = game.Workspace.Part -- getting the part 
Part.AncestryChanged:Connect(function() -- when the parent has changed this function will be fired
	print(Part.Name.." has been parented with "..Part.Parent.Name) -- prints the part's name and the part's parent's name
end)

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