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

How do i loop this with out :Clone getting looped as well?

So I have a magic script, that has two meshes, that are determined by a GUI, when the GUI changes the Mesh.Value changes. I realized the reason the mesh being used wasn't changing was due to the script running only once, and never running again with the updated values. So now i get to the problem with Looping, since whenever i start a loop either it just spams :Clone, or the variable isn't declared.

--Selection of mesh1 and mesh2
	local Mesh1 = Meshes:WaitForChild(FirstMesh.Value):Clone()
	print(Mesh1)
	Mesh1.CFrame = Humrp.CFrame * CFrame.new(0,0,-3)
	Mesh1.Orientation = Mesh1.Orientation + Vector3.new(-90,0,0)
	Mesh1.Parent = effectsfold
	
	local Mesh2 = Meshes:WaitForChild(SecondMesh.Value):Clone()
	print(Mesh2)
	Mesh2.CFrame = Mesh1.CFrame
	Mesh2.Orientation = Mesh2.Orientation + Vector3.new(-90,0,0)
	Mesh2.Parent = effectsfold

My attempts at solving this:

	while wait() do
	local Mesh1 = Meshes:WaitForChild(FirstMesh.Value):Clone()
	print(Mesh1)
	Mesh1.CFrame = Humrp.CFrame * CFrame.new(0,0,-3)
	Mesh1.Orientation = Mesh1.Orientation + Vector3.new(-90,0,0)
	Mesh1.Parent = effectsfold
	
	local Mesh2 = Meshes:WaitForChild(SecondMesh.Value):Clone()
	print(Mesh2)
	Mesh2.CFrame = Mesh1.CFrame
	Mesh2.Orientation = Mesh2.Orientation + Vector3.new(-90,0,0)
	Mesh2.Parent = effectsfold
	end

4 comments

2

Just put the clones out of the loop.

0

I've tried that, and then it says Mesh isnt declared.

0

Where's the loop

0

i posted my loop attempt that i did before hand

1 answer

sounds like you need to declare mesh 1 and 2 outside the loop but not the mesh cloning. If i read what you want correctly. The code you've supplied just clones the two messes each loop. is that correct?

The way i would get around thisproblem is declaring the varables outside the loop then check if that varible is nill or not per loop. Something like:

--Selection of mesh1 and mesh2
local Mesh1 = nil
local Mesh2 = nil 


while true do
	if(Mesh1 == nil and FirstMesh.Value ~= nil) then
		Mesh1 = Meshes:WaitForChild(FirstMesh.Value):Clone()
		Mesh1.Parent = effectsfold
	end
	print(Mesh1)
	if(Mesh1 ~= nil) then
		Mesh1.CFrame = Humrp.CFrame * CFrame.new(0,0,-3)
		Mesh1.Orientation = Mesh1.Orientation + Vector3.new(-90,0,0)
	end
	if(Mesh2 == nil and SecondMesh.Value ~= nil) then
		Mesh2 = Meshes:WaitForChild(SecondMesh.Value):Clone()
		Mesh2.Parent = effectsfold
	end
	print(Mesh2)
	if(Mesh2 ~= nil) then
		Mesh2.CFrame = Mesh1.CFrame
		Mesh2.Orientation = Mesh2.Orientation + Vector3.new(-90,0,0)
	end
	if(Mesh1 == nil or Mesh2 == nil) then
		if(Mesh1 == nil) then
			warn("Warning Mesh1 is nil! \n due to FirstMesh.Value returns"..tostring(FirstMesh.Value).."!")
		end
		if(Mesh2 == nil) then
			warn("Warning Mesh2 is nil! \n due to SecondMesh.Value returns"..tostring(SecondMesh.Value).."!")
		end
	end
	wait() 
end

Haven't tested the code but it should work like this:

  1. you define the two mesh varables
  2. in the loop you check to see if the Mesh varibles are nil if they are clone them as needed. but only if your first/second mesh string values are set if not then the script will spit out a warning in the output.

Hope this helps! :)

3 comments

0

This was a really solid attempt :D, but one thing is the :Clone gets spammed if its inside the loop

0

the clone function is only getting called once for both the meshes then once the clone function is called it sets the mesh varibles to be of the meshes and not nil. It doesn't run over and over unless your removing/deleting the meshes someplace else. Even then the script won't know about as you would have to either check the meshes parent per loop or hook up the AncestryChanged event

0

kinda like mesh.AncestryChanged:connect(function(part,newparent) if newparent == nil then [mesh1-2] = nil end end) etc...

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