I have fully functional moving screens. What I am trying to do is get 1 screen to rotate and after 2 seconds the 2nd screen starts to rotate, same thing with the 3rd and 4th screen. Somehow the button doesn’t work.
Here is the code to make them move.
local ClickerR5 = script.Parent
local Pass = script.Parent.Parent.Pass5
local Click = script.Parent.Parent.Click5
local Screen1 = script.Parent.Parent.Screen1.Move1
local Screen2 = script.Parent.Parent.Screen2.Move2
local Screen3 = script.Parent.Parent.Screen3.Move3
local Screen4 = script.Parent.Parent.Screen4.Move4
function onClicked()
if Click.Value == true then
if Pass.Value == true then
Click.Value = false
for i = 1, 393 do
Screen1.CFrame = Screen1.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen2.CFrame = Screen2.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen3.CFrame = Screen3.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen4.CFrame = Screen4.CFrame * CFrame.fromEulerAnglesXYZ(0,0,0.004)
wait()
end
Click.Value = true
Pass.Value = false
else
Click.Value = false
for i = 1, 393 do
Screen1.CFrame = Screen1.CFrame * CFrame.fromEulerAnglesXYZ(0,0,-0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen2.CFrame = Screen2.CFrame * CFrame.fromEulerAnglesXYZ(0,0,-0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen3.CFrame = Screen3.CFrame * CFrame.fromEulerAnglesXYZ(0,0,-0.004)
wait()
end
wait(2)
for i = 1, 393 do
Screen4.CFrame = Screen4.CFrame * CFrame.fromEulerAnglesXYZ(0,0,-0.004)
wait()
end
Click.Value = true
Pass.Value = true
end
end
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
while wait() do
if Pass.Value == true then
script.Parent.BrickColor = BrickColor.new("Royal purple")
else
script.Parent.BrickColor = BrickColor.new("Shamrock")
end
end
Note People are saying I need to use tweenservice, no THIS IS CFrame ITSELF. Using other moving services wont make these screens move how I want them to
UPDATE The screens do move but now its moving when the first screen completes its rotation and then the 2nd screen waits 2 seconds before moving after the 1st completed its rotation. Not how I wanted yet… I want the 2nd, 3rd, and 4th to move while the other one is moving
First of all, you CAN tween CFrames and that would probably be better than this.
Secondly, are the two conditions at the top of the onClicked() function being met?
The reason why they aren’t all moving at the same time is because you wrote it to wait for each screen to finish moving before starting the next. If you want them to move simultaneously, try putting a spawn around each one.
Not very sure if that would work… would you mind if I show you a vid on how I want them to move? This will be put in individual buttons but I want it to work like that with 1 button
You’re code is incredibly repetitive and should make use of loops. I quickly rewrote your code to make it more efficient.
local ClickerR5 = script.Parent
local Pass = script.Parent.Parent.Pass5
local Click = script.Parent.Parent.Click5
local Screens = script.Parent.Parent -- this would be better in its own folder so the extra check (marked with **) are not needed
function onClicked()
if not Click.Value then return end
Click.Value = false
local displacementZ = 0.005
if not Pass.Value then
displacementZ = -displacementZ
end
for num, screen in ipairs(Screens:GetChildren()) do -- ipairs will order the screens correctly
-- coroutine will run this function independetly.
if screen:FindFirstChild("Move .. num") then -- ** make sure this is a screen - neccesary because Pass5 and Click5 are children of Screens aswell
coroutine.wrap(function()
for i = 1, 393 do
screen["Move"..num].CFrame = screen["Move"..num].CFrame * CFrame.fromEulerAnglesXYZ(0,0,displacementZ)
wait()
end
end)()
wait(2) -- because of coroutine, next screen will rotate after 2 seconds
end
end
Click.Value = true
Pass.Value = true
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
-- a more efficent way of changing colors, because` while wait() do` is very expensive, and unnecessary.
Pass.Valued.Changed:Connect(function()
if Pass.Value then
script.Parent.BrickColor = BrickColor.new("Royal purple")
else
script.Parent.BrickColor = BrickColor.new("Shamrock")
end
end)
Of course, I’m not able to test this, so it there may possibly be a typo or something that won’t work.
As @detourious said, tweens are likely a better options for this case. Here is an example of it’s use case:
for num, screen in ipairs(Screens:GetChildren()) do
if screen:FindFirstChild("Move .. num") then
local rotate = TweenService:Create(screen["Move"..num], rotateInfo, {Rotation = Vector3.new(0, 90, 0)}) -- change to desired rotation
rotate:Play() -- tweens do not yield script, so coroutine is not needed
wait(2)
end
end
It doesnt work, even when I found some typo’s theres still errors, and from the top where do I assign the 4 screens? Are you saying I should add a folder and put the screens there?
It would be best to have all 4 screens in one folder, because for i, v in pairs() will loop through all of the children of an object. I noticed that all of the screens and Pass5 and Click5 are both children of script.Parent.Parent, so I added a check screen:FindFirstChild("Move"..num), which would unnecessary if the screens are the only thing inside of a folder.
A problem I can imagine having is when I look for the Move part inside of the screen I’m doing screen["Move"..num] because the Move parts inside the screens have numbers on them. I would recommend renaming all of the Move parts to simply “Move”, and do screen.Move inside of the loop.