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):
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.
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
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
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
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)