GOAL:
I want to dynamically add a new part inside of the Targets folder so that all of my pets will follow it. The part will be added when the player clicks on a surface (this target part placing system is already implemented).
CODES:
I will only paste relevant codes. Here is simplified code in my script (the script is a child of the pet model).
β PET MODEL SCRIPT β
local targetsFolder = game.Workspace.Targets
targetsFolder.ChildAdded:Connect(function(child)
print("Something is added to workspace")
end)
PROBLEM:
I cannot implement a move code because when I add a new part inside Targets folder, the pet model script does not run (print does not work). How can I make a code inside the pet model script wherein it triggers if a part is added to a folder?
Hello, does ChildAdded only trigger the Instance.new(βPartβ) is invoked on the same file or does it work even if Instance.new(βPartβ) is invoked on other files?
function addTarget()
local workspace = game.Workspace
local mouse = player:GetMouse()
local position = mouse.Hit.p
local target = Instance.new("Part")
target.Size = Vector3.new(1, 1, 1)
target.Position = position
target.Anchored = true
target.CanCollide = false
target.Name = "Target"
local value = Instance.new("StringValue")
value.Name = "Owner"
value.Value = player.Name
value.Parent = target
target.Parent = workspace.Targets
end
Here is my addTarget code from a LocalScript. But, whenever I add a target the ChildAdded in the pet model script does not trigger.
Changes made (creating a part in this case) on a client can only be observed by said client. The folder cannot see this because it is server sided, instead, pass information from the client to the server using remote events.
local targetsFolder = game.Workspace.Targets
targetsFolder.ChildAdded:Connect(function(child)
if child:IsA("Part") then
print("A new part has been added to the folder!")
end
end)