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
If i understood you correctly, this shall help you... i guess...
1 | local object = workspace.Model |
2 | object.ChildAdded:Connect( function (part) --runs code if a child is added |
3 | part.CanCollide = true |
4 | part.BrickColor = BrickColor.New( "Bright Yellow" ) |
5 | part.Anchored = true |
6 | --ect |
7 | end ) |
i hope that helped edit: i did not understand the question right, correct? anyways, heres what i think you want to mean:
1 | local object = workspace.Object |
2 | object:GetPropertyChangedSignal( "Parent" ):Connect( function () --runs code if parent changes |
3 | local par = object.Parent --object's parent |
4 | par.CanCollide = true |
5 | par.BrickColor = BrickColor.New( "Bright Yellow" ) |
6 | par.Anchored = true |
7 | --ect |
8 | 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.
1 | object.AncestryChanged:Connect( function (_, newParent) |
2 | print ( "Parent has been changed" ) |
3 | end ) |
--
First would be to use GetPropertyChangedSignal("Parent")
1 | local p = Instance.new( 'Part' ) |
2 | p:GetPropertyChangedSignal( 'Parent' ):Connect( function () |
3 | print ( "The parent has been changed" ) |
4 | end ) |
or .Changed and check if the property is "Parent"
1 | p.Changed:Connect( function (property) |
2 | if property = = "Parent" then |
3 | print ( "parent changed" ) |
4 | end |
5 | end ) |
You can also use .ChildRemoved on the object's parent and check if the object that got removed is the one you know.
1 | local obj = workspace.Object |
2 |
3 | workspace.ChildRemoved:Connect( function (object) |
4 | if object = = obj then |
5 | print ( "parent has been changed" ) |
6 | end |
7 | 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.
Here's an example of what you're trying to achieve. Here's a demonstration of the script.
1 | local Part = workspace.Part |
2 |
3 | local function onParentChanged() |
4 | print ( "Part's parent is now " ..Part.Parent.Name) |
5 | end |
6 |
7 | 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:
1 | local Part = game.Workspace.Part -- getting the part |
2 | Part.AncestryChanged:Connect( function () -- when the parent has changed this function will be fired |
3 | print (Part.Name.. " has been parented with " ..Part.Parent.Name) -- prints the part's name and the part's parent's name |
4 | end ) |