for i, Target in pairs(game.Workspace.Target:GetChildren()) do
if Target then
local Locate = Instance.new("Part")
Locate.Name = Locate
Locate.Parent = game.Workspace
But I also want to update the location of the Locate part based on the location of Target
while wait(2) do
for i, Locate in pairs(game.Workspace:GetChildren()) do
if Locate.Name == "Locate" then
for i, Target in pairs(game.Workspace.Target:GetChildren()) do
Locate.Position = Target.Position
if I put the loop inside creating the instance the code would just stop after making that instance. I tried to use the function method by returning the Locate variable from the first function but that also stops the code, maybe i did something wrong. In the end I want the Locate’s position to match the Target’s position
Could you explain more what is the purpose of the script?
You iterate “Target” and create a new Part for each “thing” inside “Target”, and then you set the name of the new Part to the new created instance Locate? (thats weird, whats the reason for that?)
Then on a while loop at the bottom of your code, you are iterating to find all the “Locate” parts in game, for each part found you do another loop to get the “Targets” again and Position the Locate to a random Target position found in “Targets”
Would be helpful to know what is your goal for the system you are creating in order to help you, meanwhile, I would try something like this cause idk what you are trying to achieve:
local ListOfTargets = game.Workspace.Target:GetChildren() -- Having a table of targets
local ListOfLocates = {} -- having a table of locates created
for i, Target in pairs(ListOfTargets) do
if Target then
local Locate = Instance.new("Part")
Locate.Name = Target.Name -- Maybe you want to use the name of the Target and not the Locate instance as name
Locate.Parent = game.Workspace
table.insert(ListOfLocates, Locate)
end
end
task.spawn(function()
while wait(2) do
for i, Locate in pairs(ListOfLocates) do
local findATargetRelated = game.Workspace.Target:FindFirstChild(Locate.Name)
if findATargetRelated then
Locate.Position = findATargetRelated.Position
end
end
end
end)
I am making a follow the arrow type of thing for my tutorial and I need the parts to connect to the Target point. But I wanted to make sure the distance updates everytime which was why the code was so confusing.
The reason why it is iterated to the Targets because the Locates are with their respective Targets (have multiple). Not sure if the table method works for that because they will probably be connected to that specific Target and each having their own
is just a replacement for the distance increase or decrease calculation.
If youre still confused it’s like the obejct “Beam” connected to the two attachments that updates everytime I move the attachment. I have the beam which lets say is the instance but the thing I’m trying to do is updating it but i’m not sure how to sepparate the two functions one for creating the instance and the other for updating
Yup, with the tables approach, each Locate created is kinda “linked” to the specific Target part that was the responsible for creating it from the “for loop”.
While testing that code I gave you, I created 4 parts inside a Target folder in Workspace. And 4 Locate parts are created when the for loop runs, then, the new parts falls cause are not anchored, and each 2 seconds those parts goes back to the respective Targets from where those were created
Im not sure what you mean by “is just a replacement for the distance increase or decrease calculation.”
That line only change the position of Locate and use the Target part position that were responsible for creating the Locate part. So it goes back to the Target’s position each 2 seconds.
i’m not sure how to sepparate the two functions one for creating the instance and the other for updating
If your while loop was yielding the script, thats the reason why I added task.spawn() So the updating while loop runs on a different thread and not yielding the code that is creating parts.
Yes it’s a replacement
I still have the problem everything works except all the Locator are linked to one of the Targets. Locators are located in Workspace itself not the Targets folder which contains different NPC that moves. Locators are the things connecting the player to the Targets