The script below is Localscript and is in StarterCharacterScript.
wait(1) local player = game.Players.LocalPlayer local character = player.Character local torso = character.HumanoidRootPart local mouse = player:GetMouse() character.Humanoid.AutoRotate = false mouse.Move:connect(function() torso.CFrame = CFrame.new(torso.Position, mouse.Hit.p*Vector3.new(1,0,1) + torso.Position*Vector3.new(0, 1, 0)) 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:
local ev = mouse.Move:Connect(function() --stuff end) wait(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).