Heya!! VERY new to scripting here. I haven't learnt anything about Lua grammar and what most of the values do etc. so I am trying to get there by working on small mechanics.
I am attempting to make a script in which holding left shift increases player speed and increases the Field Of View.
This is what I have:
local UIS = game:GetService("UserInputService") local player = game.Players.LocalPlayer local camera = workspace.CurrentCamera local TweenService = game:GetService("TweenService") local FOV = camera.FieldOfView FOV = 70 local sprintstart = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out) local sprintend = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.In) local startgoal = {} startgoal.FOV = 90 local endgoal = {} endgoal.FOV = 70 local starttween = TweenService:Create(FOV, sprintstart, startgoal) local endtween = TweenService:Create(FOV, sprintend, endgoal) UIS.InputBegan:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then player.Character.Humanoid.WalkSpeed = 36 starttween:Play() end end) UIS.InputEnded:Connect(function(input, gameProcessedEvent) if input.KeyCode == Enum.KeyCode.LeftShift then player.Character.Humanoid.WalkSpeed = 16 endtween:Play() end end)
This only results in the error 'Unable to cast value to object', which I can probably assume is just a grammar area, or something I haven't defined. Sadly I am too amateur to figure this one out, so if any of you guys could help me that would be great! Thank you!!
EDIT: the error only came up once I attempted to tween the FOV, the script worked fine without the tweening.
This is the script I use:
local UserInputService = game:GetService("UserInputService") local Players = game:GetService("Players") local Workspace = game:GetService("Workspace") local localPlayer = Players.LocalPlayer UserInputService.InputBegan:Connect(function(input, gameProcessed) if not gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift then localPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 36 Workspace.CurrentCamera.FieldOfView = 90 end end) UserInputService.InputEnded:Connect(function(input, gameProcessed) if not gameProcessed and input.KeyCode == Enum.KeyCode.LeftShift then localPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 16 Workspace.CurrentCamera.FieldOfView = 70 end end)
You don't have to use tweening since you can just change the FOV of workspace.CurrentCamera
.
Although, if you WANT to use tweening then you should use this:
local UserInputService = game:GetService("UserInputService") local Workspace = game:GetService("Workspace") local TweenService = game:GetService("TweenService") local Players = game:GetService("Players") local localPlayer = Players.LocalPlayer local humanoid = localPlayer.Character:WaitForChild("Humanoid") local defaultFOV = 70 local properties1 = {FieldOfView = defaultFOV + 20} local info1 = TweenInfo.new( 0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut ) local tween1 = TweenService:Create(Workspace.CurrentCamera, info1, properties1) local properties2 = {FieldOfView = defaultFOV} local info2 = TweenInfo.new( 0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut ) local tween2 = TweenService:Create(Workspace.CurrentCamera, info2, properties2) UserInputService.InputBegan:Connect(function(input, gameProccesed) if not gameProccesed and input.KeyCode == Enum.KeyCode.LeftShift then humanoid.WalkSpeed = 36 tween1:Play() end end) UserInputService.InputEnded:Connect(function(input, gameProccesed) if not gameProccesed and input.KeyCode == Enum.KeyCode.LeftShift then humanoid.WalkSpeed = 16 tween2:Play() end end)
Edit: You should use tweenservice and to change FOV, you'd have to change the property of workspace to FOV. Also, just listen to youtubemasterwow's answer as it is essentially the same thing, but detailed.
local DefaultFoV = 70 local Properties = {FieldOfView = DefaultFoV + 10} local Info = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut) -- edit as you want local T = game:GetService("TweenService"):Create(game.Workspace.CurrentCamera, Info, Properties) T:Play()