An issue with my code

so, my code should rotate object in viewport if it’s added or if it’s exist.

Here is the part of code which is a adding object to the view port:

– script type: basic script


	local toolClone3 = tool.H1:Clone()
	toolClone3.Parent = clonedTemplate.ViewportFrame

after this function “ancestryAdded” is triggered, and searching for a speacial object.

– script type: basic script

-- Define a function to rotate a part
local function rotatePart(part)
	
	print("rotated")
	
	while wait(0.001) do
		part.CFrame *= CFrame.Angles(0, 0, 0.05)
	end
end

-- Connect the function to the H1 part (if it exists)
if script.Parent:FindFirstChild("H1") then
	print("H1 exists on the original object")
	rotatePart(script.Parent.H1)
end

-- Connect the function to the ViewportFrame's children (for both original and cloned objects)
script.Parent.AncestryChanged:Connect(function()
	local hand = script.Parent:FindFirstChild("Hand")
	if hand then
		local viewportFrame = hand:FindFirstChildWhichIsA("ViewportFrame")
		if viewportFrame then
			for _, child in ipairs(viewportFrame:GetChildren()) do
				if not child:IsA("Script") then
					print("Found a non-script child in ViewportFrame")
					rotatePart(child)
				end
			end
		end
	end
end)

after this all works! The part is rotating and I see all prints, but whats the problem, In my other code this viewport should be dublicated, here is part of code which is a dublicated viewport (with a part that newly added):

script type: local script

		VFclone = VF:Clone()
		VFclone.Parent = ItemPhoto.Parent.ViewFrame

It should make a check if part “H1” exist (I 100% it exist, I’ve see it myself in playerGui) but It won’t rotate! Please help me, maybe I do some mistakes.

1 Like

Hello! I have had issues like this and just couldn’t work it out. Normally it’s something so stupid that we just overlook it, however I can only give tips to find it:

  • Ensure you’ve named the Locals correctly
  • Ensure “VFclone” has a position set as they don’t always end in same position as you’d like
  • Sometimes rescripting helps.

Hope this helps!

1 Like

Can I show you what script should


do?

1 Like

It must to rotate part that got copied in the another frame

1 Like

Try doing the rotation with the Position instead of CFrame

1 Like

Nah still doesn’t work, the problem that script works correctly but when I’t got copied it stops

1 Like

Try making it so it finds the part when the mouse enters the UI of which needs to rotate!

I modified this script

print( math.random( -10, 10 ))

local function rotatePart(part)
	
	print("rotated")
	
	while wait(0.001) do
		local rotationAngle = CFrame.Angles(0, 0, math.rad(1)) -- Adjust the rotation angle as needed
		part.CFrame = part.CFrame * rotationAngle
		wait(0.01) -- Adjust the wait time as needed
	end
end

-- Connect the function to the H1 part (if it exists)
if script.Parent:FindFirstChild("H1") then
	print("H1 exists on the original object")
	rotatePart(script.Parent.H1)
end

-- Connect the function to the ViewportFrame's children (for both original and cloned objects)
script.Parent.AncestryChanged:Connect(function()
	local hand = script.Parent:FindFirstChild("Hand")
	if hand then
		local viewportFrame = hand:FindFirstChildWhichIsA("ViewportFrame")
		if viewportFrame then
			for _, child in ipairs(viewportFrame:GetChildren()) do
				if not child:IsA("Script") then
					print("Found a non-script child in ViewportFrame")
					rotatePart(child)
				end
			end
		end
	end
end)

and I see random number only after AbcestryChanged

Bruh this scripts runs only I don’t know how

print( math.random( -10, 10 ))

local function rotatePart(part)
	
	print("rotated")
	
	while wait(0.001) do
		local rotationAngle = CFrame.Angles(0, 0, math.rad(1)) -- Adjust the rotation angle as needed
		part.CFrame = part.CFrame * rotationAngle
		wait(0.01) -- Adjust the wait time as needed
	end
end

-- Connect the function to the H1 part (if it exists)
if script.Parent:FindFirstChild("H1") then
	print("H1 exists on the original object")
	rotatePart(script.Parent.H1)
end

this code runs only when H1 object adding to the Viewport

It’ll need to be modified like this I believe

print( math.random( -10, 10 ))

local function rotatePart(part)
	
	print("rotated")
	
	while wait(0.001) do
		local rotationAngle = CFrame.Angles(0, 0, math.rad(1)) -- Adjust the rotation angle as needed
		part.CFrame = part.CFrame * rotationAngle
		wait(0.01) -- Adjust the wait time as needed
	end
end

-- Connect the function to the H1 part (if it exists)
GUIHERE.MouseEnters:Connect(Function)(
if script.Parent:FindFirstChild("H1") then
	print("H1 exists on the original object")
	rotatePart(script.Parent.H1)
end
end)

-- Connect the function to the ViewportFrame's children (for both original and cloned objects)
script.Parent.AncestryChanged:Connect(function()
	local hand = script.Parent:FindFirstChild("Hand")
	if hand then
		local viewportFrame = hand:FindFirstChildWhichIsA("ViewportFrame")
		if viewportFrame then
			for _, child in ipairs(viewportFrame:GetChildren()) do
				if not child:IsA("Script") then
					print("Found a non-script child in ViewportFrame")
					rotatePart(child)
				end
			end
		end
	end
end)

Thanks for helping but I need to make it rotate automaticly.

1 Like

after cloning viewport frame you need to call rotatepart function again as it doesnt get cloned
edit:
image

Thanks, but why it doesn’t work? I got an check if H1 exist then function is fire, is there like any other events that could help me?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.