How can I break out of the loop

  1. What do you want to achieve?
    I want to make the camera so that the animation plays and when it ends the camera goes back towards the player
  2. What is the issue? Include screenshots / videos if possible!
    The problem is the camera is either the camera animation plays but my camera switches instantly or the players camera gets stuck to the camera rig part.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I used a while loop with a Boolean but the loop will continuously run forever. If I tried changing the Boolean in the loop it buffers and the camera is stuck to one frame. I tried to make it so the while loop breaks when it stops the animation but same problem. I tried using a repeat until loop same problem. I also tried to duplicate the the camera rig so I can destroy it but didn’t know how I would destroy it even if I tried it didnt work.

I put this in a local script under StarterCharacterScripts

local CamA = script.Parent.TakeDownCam
local UserA = script.Parent.TakeDownYou
local EnemyA = script.Parent.TakeDownEnemy
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character =  Player.Character
local UIS = game:GetService("UserInputService")
local Enemy = workspace.Opps
local CamRigAnimator = workspace.Camera_Rig.Humanoid:WaitForChild("Animator")
local CamRigHum = CamRigAnimator.Parent
local Camera = workspace.Camera
local IsPlaying = false


UIS.InputBegan:Connect(function(input, GPE)
	
	if GPE then return end
	
	if input.KeyCode == Enum.KeyCode.G then
		
		IsPlaying = true
		Enemy.Archivable = true
		local OpCloned = Enemy:Clone()
		OpCloned.Parent = game.Workspace
			
		for i, v in ipairs(OpCloned:GetChildren())  do
			
			if v:IsA("Part") then
				
				for i, c in ipairs(Character:GetChildren()) do
					
					if c:IsA("Part") then
						
						v.Parent.HumanoidRootPart.CFrame = c.Parent.HumanoidRootPart.CFrame * CFrame.new(0,0,-5) * CFrame.Angles(0,math.rad(180), 0)
						
						
						
					end
					
				end
				
				
			end
			
			
		end	
		
		local CamT = CamRigAnimator:LoadAnimation(CamA) 
		CamT:Play()
		local CamRigClone = CamRigHum.Parent.Torso:Clone()
		CamRigClone.Parent = workspace
		
	while IsPlaying do
			
		task.wait()
		Camera.CameraType = Enum.CameraType.Scriptable
		Camera.CFrame = CamRigClone.CFrame	
		if CamT.Ended then
               IsPlaying = false
               break
end	
	end	
	
	Camera.CameraType = Enum.CameraType.Custom	
		
end
	

end)

2 Likes

In my opinion, I think it’s best to use RunService.RenderStepped or RunService.Heartbeat. It acts pretty much like a while loop but instead you connect it to a function, so that way code below it can run. However, I don’t think you can use task.wait() or wait() in it though. But I’ve been able to make a ton of stuff using Heartbeat or RenderStepped instead of while. Not sure if this will help you or not, but good luck on your project!

1 Like

Why is run service useful though??

It is FPS based, it also has a delta time parameter on the .HeartBeat event so you can make the things look equal on different frame rates.

1 Like

Like I know that much but like when should it be used

When you want to make something be the same no matter the user’s refresh rate.

1 Like

You should especially use it with the camera

to turn it off after you dont need it just :Disconnect() the connection

local connection = game:GetService("RunService").RenderStepped:Connect(function(delta)
--update the camera CFrame here
end)

and just do

connection:Disconnect()

whenever the cutscene is over

1 Like

Additional info for you, use RunService:BindToRenderStep to have priority based function calls.

1 Like

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