So I've been trying to make the character face towards the mouse cursor when activating a tool. I wanted to interpolate the changing of the CFrame and make it a tween so there's a slight interpolation before the character rotates towards the cursor, making it overall look and feel nicer.
I've ran into a problem with my tween constantly playing itself in the Heartbeat loop and I need it to play with the Time parameter only once and then you could say the Time parameter is 0.
This is my code:
local tool = script.Parent local camera = game:GetService("Workspace").CurrentCamera local runService = game:GetService("RunService") local plr = game:GetService("Players").LocalPlayer local char = plr.Character or plr.CharacterAdded:Wait() local toolActivated = false local mouse = plr:GetMouse() local UserInputService = game:GetService("UserInputService") local TweenService = game:GetService("TweenService") tool.Activated:Connect(function() toolActivated = true end) tool.Deactivated:Connect(function() toolActivated = false end) runService.Heartbeat:Connect(function() if toolActivated then local HumanoidRootPart = char:WaitForChild("HumanoidRootPart") local HRPpos = HumanoidRootPart.Position local goal = {} goal.CFrame = CFrame.new(HRPpos, Vector3.new(mouse.Hit.Position.x, HRPpos.y, mouse.Hit.Position.z)) local tweenInfo = TweenInfo.new(0.1) local lookTween = TweenService:Create(HumanoidRootPart, tweenInfo, goal) lookTween:Play() end end)
As you can see, I have made a variable containing tween information for a tween I made. I need to somehow find a way to change it after the tween plays for the first time when you only activate the tool so that the 0.1 I wrote (Time parameter) is 0 the second time the tween runs so that there is no interpolation. How can I achieve that?
Tweens aren't meant for animations whose values are constantly updating. Instead, use a BodyGyro.
local gyro = Instance.new("BodyGyro") -- create a body gyro --change the properties of the gyro to your content tool.Activated:Connect(function() toolActivated = true gyro.Parent = HumanoidRootPart --parent gyro to HRP when tool equipped end) tool.Deactivated:Connect(function() toolActivated = false gyro.Parent = nil --unparent gyro when tool unequipped end) game:GetService("RunService").Stepped:Connect(function() if Humanoid.Health > 0 and ToolActivated then -- check if player is alive and has tool equipped if gyro.Parent == HumanoidRootPart then gyro.CFrame = CFrame.new(pp.Position, Vector3.new(mouse.Hit.p.X, pp.Position.Y, mouse.Hit.p.Z)) end end end) Humanoid.Died:Connect(function() gyro:Destroy() -- so u don't have a corpse spinning right round baby right round end)