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

How do I create a custom sprint script, as well as change the FOV so that it is higher?

I'm working on a game for a friend, and he asked me to make a sprint script. As I didn't want to use an already made one, I attempted to make one myself, which doesn't shoot any errors when I use it, but it does not change the speed. I do not know how to change the FOV either. Please help!

UIS = game:GetService("UserInputService")

function sprint()
	local debounce = 1
	if debounce then
		--make fov
		debounce = 0
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 24
		wait(0.1)
		debounce = 1
	end
end

function stopsprint()
	game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
end

game.Players.PlayerAdded:Connect(function(plr)
	UIS.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.KeyCode.LeftShift or input.UserInputType == Enum.KeyCode.RightShift then
			sprint()
		end
	end)
	UIS.InputEnded:Connect(function(input)
		if input.UserInputType == Enum.KeyCode.LeftShift or input.UserInputType == Enum.KeyCode.RightShift then
			stopsprint()
		end
	end)
end)

2 comments

0

Try using remotes to change walkspeed.

0

posted the working code

2 answers

✓ Accepted answer

You are attempting to change the walkspeed in a localscript. This can be fixed with RemoteEvents. You can also change FOV by taking the workspace's currentcamera.

Localscript:

local UIS = game:GetService("UserInputService")
local Event = EventPath:FindFirstChild("EventNameHere") -- make a remoteevent in the path.
local Camera = game.Workspace.CurrentCamera

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		Event:FireServer("Start")
		Camera.FieldOfView = 90 -- You can change this
        end
end)
UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift or input.KeyCode == Enum.KeyCode.RightShift then
		Event:FireServer("Stop")
		Camera.FieldOfView = 70 -- Default FOV
        end
end)

Serverscript:

local Event = EventPath:FindFirstChild("EventNameHere")

Event.OnServerEvent:Connect(function(player,toggle) -- Listen to event
	if player then -- Check if player hasn't left (not necessary)
		local character = player.Character
		local humanoid = character:FindFirstChild("Humanoid")
		if toggle == "Start" then
			humanoid.WalkSpeed = 24
		elseif toggle == "Stop" then
			humanoid.WalkSpeed = 16
		end
	end
end)

This should work (fixed code)

1 comment

I'd use TweenService with the FOV changer as it looks smoother than just changing the camera's FOV. Make sure it's a LocalScript inside StarterPlayer > StarterCharacterScripts. Try 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 + 4}

local info1 = TweenInfo.new(
	0.25, 
	Enum.EasingStyle.Bounce,
	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, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 24
		tween1:Play()
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent and input.KeyCode == Enum.KeyCode.LeftShift then
		humanoid.WalkSpeed = 16
		tween2:Play()
	end
end)

TweenService can be a bit hard to explain, so I'll leave the Roblox Developer Hub link for you.

1 comment

0

It still does nothing. It's a script and it's in workspace.

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