Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to check if something has been parented?

Asked by 5 years ago

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

0
Does the part already have a parent? co_existance 141 — 5y
0
You could set a variable equal to the current parent and use a while loop to check if it's changed? killerbrenden 1537 — 5y

4 answers

Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
5 years ago
Edited 5 years ago

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

1local object = workspace.Model
2object.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
7end)

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

1local object = workspace.Object
2object: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
8end)

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

Ad
Log in to vote
1
Answered by
Elixcore 1337 Moderation Voter
5 years ago
Edited 5 years ago

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.

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

--

First would be to use GetPropertyChangedSignal("Parent")

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

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

1p.Changed:Connect(function(property)
2    if property == "Parent" then
3        print("parent changed")
4    end
5end)

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

1local obj = workspace.Object
2 
3workspace.ChildRemoved:Connect(function(object)
4    if object == obj then
5        print("parent has been changed")
6    end
7end)
Log in to vote
0
Answered by
y3_th 176
5 years ago

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.

1local Part = workspace.Part
2 
3local function onParentChanged()
4    print("Part's parent is now "..Part.Parent.Name)
5end
6 
7Part:GetPropertyChangedSignal("Parent"):Connect(onParentChanged) --will print the part's parent's name every time it is changed
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

1local Part = game.Workspace.Part -- getting the part
2Part.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
4end)

Answer this question