✓ Accepted answer
The problem is that you're not playing the Tween. A Tween object has a Play() function which does what you think; it plays the tween.
However, to accomplish this we will need to reference the tween somehow, so we just save it as a local variable.
local tweenService = game:GetService("TweenService")
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
userInput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
local t = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80})
t:Play()
script.Parent.Humanoid.WalkSpeed = 30
end
end)
userInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
local t = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
t:Play()
script.Parent.Humanoid.WalkSpeed = 16
end
end)
Even with this, there's still room for improvement. If a player were to stop running shortly after having started running it will look a bit weird. So to fix this you simply use the Cancel() function of the Tween object to reset the animation.
I used some variables so that each tween is "Saved" but also readable by the entire script, thus solving our problem.
local tweenService = game:GetService("TweenService")
local userInput = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local t0
local t1
userInput.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
t0 = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 80})
t0:Play()
if t1 then
t1:Cancel()
end
script.Parent.Humanoid.WalkSpeed = 30
end
end)
userInput.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
t1 = tweenService:Create(cam, TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut), {FieldOfView = 70})
t1:Play()
t0:Cancel()
script.Parent.Humanoid.WalkSpeed = 16
end
end)
1 comment
How about using a for loop