Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I change the Time parameter in the TweenInfo variable the second time the tween plays?

Asked by 3 years ago

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?

0
It could be also useful if there is a way to just prevent the player from interrupting the tween if they press any other movement keys to strafe, making the tween replay and result in the player not facing the mouse. MrDarkLord1234 7 — 3y
0
I came to a conclusion that I just need to stop the Tween and then keep changing the CFrame constantly but now I've got another problem which is that there's a jittery delay when the tween happens and it stops the character in place and then you can walk again. https://gyazo.com/3cf1ad4a90ebad6368931a24a94493e2 Here's a gif showing what I mean and my new code: https://pastebin.com/80N5jdtW MrDarkLord1234 7 — 3y
0
The reason being that you defined the HRP's position in the tween and so the HRP freezes in that spot for 0.1 second. Tweens aren't applicable to your situation LeedleLeeRocket 1257 — 3y
0
I see. Thank you. MrDarkLord1234 7 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
Hm, I did this but my character isn't rotating consistently and most of the time the character won't look all the way back. This is my code: https://pastebin.com/830xgGZM and this is a demonstration of it: https://gyazo.com/bef5f3ede702d14724dfb003e89e14ae (P.S: When the character rotates towards the camera that's when I activate the tool and the BodyGyro fails to rotate the character. MrDarkLord1234 7 — 3y
0
Instead of connecting the function to everytime the mouse moves, bind it to runservice.stepped. I'll edit the script rq LeedleLeeRocket 1257 — 3y
0
That way it's checking to see if the tool is activated or not every frame. I only changed line 15 LeedleLeeRocket 1257 — 3y
0
You should also check if the gyro is parented to the HRP when tool is activated. I added the line on line 17 LeedleLeeRocket 1257 — 3y
View all comments (6 more)
0
I see, thank you, but wouldn't it be inefficient to run a RenderStepped loop for this? I am using a local script so it should not be a problem and I guess it isn't but just asking since I'm not as experienced. MrDarkLord1234 7 — 3y
0
I'm going to be 100% honest with you, I have no idea when to use Heartbeat, Stepped, or RenderStepped. You can use RenderStepped if you want, they all fire every frame as far as I'm concerned. LeedleLeeRocket 1257 — 3y
0
Hm, I see. Yeah, all I know is that you use heartbeat for less important things that shouldn't be done first when the server starts and RenderStepped is for calculations that need to happen as fast as possible. Also, the first time I force the player to rotate he doesn't do that, but the second time it works all fine. What's the reason for this? All I've changed was the loop on the 30th line. MrDarkLord1234 7 — 3y
0
30th line of my script, by the way. MrDarkLord1234 7 — 3y
0
I misread your question above lol. You can use while true loops but they are slightly slower. It's all preference, I guess. I'm still relatively new to scripting so I don't know all that much about performance. Anyway, I don't see a loop in your script on the 30th line? LeedleLeeRocket 1257 — 3y
0
Yeah, while loops are slower and were more apparent in older roblox games. Anyways, I meant the newer pastebin link but I guess it would be better if I just sent you my actually new script with that mouse loop changed to a RenderStepped one. https://pastebin.com/DUFicChv MrDarkLord1234 7 — 3y
Ad

Answer this question