Run if statement forever then stop then again

  1. What do you want to achieve?
    A fix or method for my script

  2. What is the issue?
    I am making a game. And wanted to add cutscenes for people on special teams
    Like a police role when you get the role after the game starts a small cutscene will start and after wards should drop the player at a location and the car should leave i have made that. But i want it to repeat again after they die or win and get the role agian. when they die or win they get changed to a spectator team. and repeats

  3. What solutions have you tried so far?
    I’ve looked at many devfourm and some YouTube videos.
    I have tried to use while true loop but i prefer not as it only does it once.
    I’ve tried coroutine.wrap
    and more

Here is a script of one of the cutscenes

local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChild("Humanoid")

local Camera = workspace.CurrentCamera

local SelectedTeam = "E-11"

local ModelCamera = workspace.Game.Cutscense.SpawnCutscenes["E-11"].Camera
local CutOne = ModelCamera:WaitForChild("One")
local CutTwo = ModelCamera:WaitForChild("Two")
local CutThree = ModelCamera:WaitForChild("Three")
local CutFour = ModelCamera:WaitForChild("Four")
local CutFive = ModelCamera:WaitForChild("Five")

local Tweeninfo = TweenInfo.new(1,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false,0)
local Tweeninfohelicopter = TweenInfo.new(10,Enum.EasingStyle.Quart,Enum.EasingDirection.Out,0,false,0)

local LandingHelicopter = workspace.Game.Cutscense.SpawnCutscenes["E-11"].Helicopter:WaitForChild("LandingHeli")

local SpawnLocation = workspace.Game.Cutscense.SpawnCutscenes["E-11"].SpawnLocation
local LocationOne = SpawnLocation.One
local LocationTwo = SpawnLocation.Two
local LocationThree = SpawnLocation.Three
local LocationFour = SpawnLocation.Four

if player.Team.Name == SelectedTeam then	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = CutTwo.CFrame
	wait(3)
	TweenService:Create(Camera,Tweeninfo, {CFrame = CutOne.CFrame}):Play()
	wait(3)
	TweenService:Create(Camera, Tweeninfo, {CFrame = CutThree.CFrame}):Play()
	wait(3)
	Camera.CFrame = CutFour.CFrame
	wait(1)
	TweenService:Create(LandingHelicopter, Tweeninfohelicopter, {CFrame = CFrame.new(132.191, 161.906, -187.081)}):Play()
	wait(9)
	TweenService:Create(LandingHelicopter, Tweeninfohelicopter, {CFrame = CFrame.new(132.191, 13.893, -187.081)}):Play()
	TweenService:Create(Camera, Tweeninfohelicopter, {CFrame = CutFive.CFrame}):Play()
	wait(10)
	local RandomSpot = math.random(1,4)
	if RandomSpot == 1 then
		Character:MoveTo(LocationOne.Position)
	elseif RandomSpot == 2 then
		Character:MoveTo(LocationTwo.Position)
	elseif RandomSpot == 3 then
		Character:MoveTo(LocationThree.Position)
	elseif RandomSpot == 4 then
		Character:MoveTo(LocationFour.Position)
	end
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	wait(2)
	TweenService:Create(LandingHelicopter, Tweeninfohelicopter, {CFrame = CFrame.new(132.191, 126.374, -187.081)}):Play()
	wait(6)
	TweenService:Create(LandingHelicopter, Tweeninfohelicopter, {CFrame = CFrame.new(120.446, 716.487, 2017.875)}):Play()
	wait(8)
	LandingHelicopter.Position = Vector3.new(-187.176, 169.502, -697.9)
	
end
1 Like

You can use player:GetPropertyChangedSignal(“Team”) to detect when the player’s team changes. When it does, you can run the if statement to see if you should play the cutscene.

(Also, you’ll probably want to use task.wait() instead of wait())

2 Likes

If your intention is to run the cutscene once after a certain event, I recommend that you use Functions.

As for a list of all events that you can use, you can find information here:

For example:

local function cutscene()
	-- Cutscene code goes here
end

game.Players.PlayerAdded:Connect(cutscene)

---------------------
-- ALTERNATIVELY
---------------------

game.Players.PlayerAdded:Connect(function()
	-- Cutscene code goes here
end)
2 Likes

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