Tween not working

I am creating a run script, and I am also trying to tween the camera zoom when it starts running. However, it isn’t working. Here is my code which is in StarterCharacterScripts. It’s giving me the error message Unable to cast dictionary to TweenInfo. I have tried changing the variable from the global one with no luck.

-- Object variables
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid
local camera = workspace.CurrentCamera

-- Services
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

-- Service Variables
local TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 32
		TS:Create(camera, TweenInfo, {FieldOfView = 100})
		
		while UIS:IsKeyDown(Enum.KeyCode.LeftShift) do wait()
			hum.WalkSpeed = 32
			TS:Create(camera, TweenInfo, {FieldOfView = 100})
		end
		hum.WalkSpeed = 16
		TS:Create(camera, TweenInfo, {FieldOfView = 70})
	end
end)

2 Likes

you will need to call the Play() method on the tween object
do

instead of

TS:Create(camera, TweenInfo, {FieldOfView = 100})
1 Like

Umm, well, I noticed some arguments are missing from the tweeninfo, but that may not matter.

Oh wait nevermind. Thats definitely the issue.

It still gives me the error “Unable to cast dictionary to TweenInfo”

Is that the only error?

If so, it should tell you what line it is on. That will help us.

change {FieldOfView = 100} to {[“FieldOfView”] = 100}

1 Like

The error says it is on line 17.

not sure if this will work but maybe try giving the TweenInfo variable a different name from the global one?

I tried this already, sorry I forgot to write it in the original post

1 Like

Try this.

-- Object variables
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid
local camera = workspace.CurrentCamera

-- Services
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

-- Service Variables
local TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 32
		TS:Create(camera, TweenInfo, {["FieldOfView"] = 100}):Play()
		
		while UIS:IsKeyDown(Enum.KeyCode.LeftShift) do wait()
			hum.WalkSpeed = 32
			TS:Create(camera, TweenInfo, {["FieldOfView"] = 100}):Play
		end
		hum.WalkSpeed = 16
		TS:Create(camera, TweenInfo, {["FieldOfView"] = 70}):Play()
	end
end)
3 Likes

You forgot some function call arguments, but after adding them it works! Mind me asking what you changed?

2 Likes

I tried the script under starter player scripts clicked shift and it didn’t make any errors

-- Object variables
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera

-- Services
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

-- Service Variables
local TweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = 32
		TS:Create(camera, TweenInfo, {FieldOfView = 100}):Play()

		while UIS:IsKeyDown(Enum.KeyCode.LeftShift) do wait()
			hum.WalkSpeed = 32
			TS:Create(camera, TweenInfo, {FieldOfView = 100}):Play()
		end
		hum.WalkSpeed = 16
		TS:Create(camera, TweenInfo, {FieldOfView = 70}):Play()
	end
end)

I did what he/she said here and added the Play() function to the end of each tween. :smile:

Glad I could help!

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.