The script below is Localscript and is in StarterCharacterScript.
01 | wait( 1 ) |
02 | local player = game.Players.LocalPlayer |
03 | local character = player.Character |
04 | local torso = character.HumanoidRootPart |
05 | local mouse = player:GetMouse() |
06 |
07 | character.Humanoid.AutoRotate = false |
08 |
09 | mouse.Move:connect( function () |
10 | torso.CFrame = CFrame.new(torso.Position, mouse.Hit.p*Vector 3. new( 1 , 0 , 1 ) + torso.Position*Vector 3. new( 0 , 1 , 0 )) |
11 | end ) |
How do I make the script stop working after a few seconds? and if you look at other direction while walking, humanoid tries to look at direction they are walking. How do I fix both of the problem?
Hi. What you're asking for is disconnecting the event listener from the Move event.
This can be done by defining the connection as a variable, and then using :Disconnect()
on it, like so:
1 | local ev = mouse.Move:Connect( function () |
2 | --stuff |
3 | end ) |
4 | wait( 5 ) |
5 | ev:Disconnect() |
Additionally, if you want to prevent the character from looking in another direction, place a BodyGyro
under Torso
or UpperTorso
, set its MaxForce
to inf on the Y axis (Vector3.new(0,math.huge,0)
), and then, instead of setting the HumanoidRootPart's CFrame, set the CFrame of BodyGyro (make sure to remove it afterwards).