How would you end this script?

Hi all,
Im making a script and Im wondering in which way would I end it (end, end), ect.)

Here is the script:

local cameraInterpolateEvent = game.ReplicatedStorage.Remotes.cameraInterpolateEvent
local cameraToPlayerEvent = game.ReplicatedStorage.Remotes.cameraToPlayerEvent



local function move_npc(npc,cp)
local maxWaitTime = 15
local walkController = npc.Humanoid:LoadAnimation(walkAnim)
walkController:Play()
npc.Humanoid:MoveTo(cp.Position.)
repeat 
      wait(1) 
     maxWaitTime = maxWaitTime - 1
until (npc.HumanoidRootPart.Position - cp.Position).magnitude < 2.5 or maxWaitTime <= 0
walkController:Stop()
end 

wait(5)

cameraInterpolateEvent:FireAllClients(game.Workspace.CameraGroup.CameraPart.CFrame, 
game.Workspace.CameraGroup.TargetPart.CFrame, 1)
2 Likes

end is a precise part of lua’s syntax.

end is the token used to mark where a certain type of programming method ends. These methods are usually ones tied to selection or iteration, but also include functions.

Each time a function, loop (some exceptions like the repeat loop uses until instead) or if statement is declared, it must be followed by an end in order for it to be valid code to run. Most people usually use tabbing or spacing to align the code to the opening statements and the end statements which makes it clear to see is happening in the code.

It might be worth noting that scripts don’t have to end with an “end”. If a sequence of programming lines exist, and the end of the script doesn’t happen to be the end of a function, loop, branch etc. then it doesn’t need an end.

Put
end
After the only end

There should be two ends there.

I put 2 ends in the form of this

End
End

And the script didnt work

Im kind of confused… do you just mean you want the script to just… stop? Or do you want to end the function?

This would fix any errors about an “end” not being there. If you just want the script to stop, just call break in the repeat loop OR it will just stop on its own once the code has been run

local function move_npc(npc,cp)
   local maxWaitTime = 15
   local walkController = npc.Humanoid:LoadAnimation(walkAnim)
   walkController:Play()
   npc.Humanoid:MoveTo(cp.Position.)
 repeat
   maxWaitTime = maxWaitTime - 1
   wait(1)
   until (npc.HumanoidRootPart.Position - cp.Position).magnitude < 2.5 or maxWaitTime <= 0
end
walkController:Stop()
end