Script getting stuck after a certain point. Please help!

  1. What do I want to achieve? I’m trying to make a jumpscare script for my horror game.

  2. What is the issue? The script freezes after a certain point. No idea why that happens. :smiley:

  3. What solutions have I tried so far? Honestly, I have tried everything. Rearranging the code, removing some parts of the code, and searching the hub, but nothing has worked so far.

Here is the script: (It’s a local script inside StarterGui)

-- Variables
local jumpscaring = false

local tweenService = game:GetService("TweenService")
local jumpscareTime = 4.65

local DeathCamEvent = game:GetService("ReplicatedStorage").RemoteEvents:FindFirstChild("DeathCamera")

local point1 = workspace.Map.JumpscareScene.Camera1
local point2 = workspace.Map.JumpscareScene.Camera2

local jumpscareMonster = workspace.Map.JumpscareScene:FindFirstChild("JumpscareMonster")
local jumpscareAnim = jumpscareMonster:FindFirstChild("JumpscareAnim")

local Camera = workspace.CurrentCamera

local Humanoid = game:GetService("Players").LocalPlayer.Character:WaitForChild("Humanoid")

-- The script
DeathCamEvent.OnClientEvent:Connect(function()
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = point1.CFrame
	jumpscaring = true
	jumpscareMonster.Humanoid:LoadAnimation(jumpscareAnim):Play()
	wait(0.2)
	script.Swing:Play()
	wait(0.2)
	script.Hit:Play()
	wait(1.6)
	script.JumpscareSound:Play()
	
	coroutine.wrap(function() -- Move the Camera back and fourth.
		repeat 
tweenService:Create(workspace.CurrentCamera,TweenInfo.new(0.1,Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{CFrame = point2.CFrame,}):Play()
			wait(0.1)
tweenService:Create(workspace.CurrentCamera,TweenInfo.new(0.1,Enum.EasingStyle.Linear, Enum.EasingDirection.Out),{CFrame = point1.CFrame,}):Play()
			wait(0.1)
		until jumpscaring == false
	end)()
	
	print("Waiting...")
	wait(jumpscareTime) -- This is where it gets stuck!

	-- Set the camera back to custom
	script.JumpscareSound:Stop()
	jumpscaring = false
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	
	if game.Players.RespawnTime >= jumpscareTime then
		Camera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	end
end)

If you have a solution for this, please let me know since I have no idea how to fix this. :smiling_face_with_tear:

Seems like you never called the wrap function try doing this

local JumpscareMove = coroutine.wrap(function() 
-- Code stuff here
end)

JumpscareMove()

You are able to call a wrap function the way that they are doing it.

coroutine.wrap(function()
–Still is called
end)()