Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Changing FOV + Player Speed on input?

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.

4 comments

0

Here is your problem(maybe), TweenService:Create(FOV, sprintstart, startgoal) instead of FOV, maybe it should be the camera.

0

that brought me back to 'attempting to index nil (fieldofvalue). but thanks anyways!

0

Sorry for the really long reply, but the error means that the arguments you passed in was a value instead of an object.

0

FOV is a value and returns a float not an object, hence the error!

2 answers

✓ Accepted answer

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)


10 comments

0

the script you posted originally was the one i had first, so I will try the second script out when I get the chance! thanks

0

ah! it worked perfectly! thank you so much!

0

No problem.

0

no? this person helped me implement it, whilst you just provided the code. it isn't even the same anyways baha

0

I did not copy it at all. Instead, I actually put work into it by explaining some things while you're just accusing me of copying your answer and as ittyisaduck said, you just provided code that you did not explain to him and how to implement it into his code. Just because you answered first doesn't mean that I copied you.

0

... Your profile says "I always use the WIKI everytime." I ALSO used the wiki to solve this question which is probably why we had similar answers.

0

I used the Roblox Developer Forums.

0

Yes.

0

@ittyisaduck, keep in mind Teenservice isn't the only way to achieve this, you could have used a for loop, but that would return a gnarly memory leak.

0

First comment in over a year.

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()

3 comments

0

its def tweening the camera but idk how to implement it bahah, but thanks!

0

Wdym.

0

This is tweenservice.

This discussion is preserved as it was. New answers, comments, and votes are closed.