This module is client-only. This script will not work on the server.Ā
This script is the property of Brado Studios. This work is governed by the following license:Ā
Copy the module to StarterPlayerScripts or Replicated Storage
Require the module in a local script
require(game:GetService("StarterPlayerScripts").sprintScript);
If you wish to change the config table, require the module to a variable
local config = require(game:GetService("StarterPlayerScripts").sprintScript);
Defaults to player walk speed. Change this to your desired walk speed in studs/sec.Ā
config.WalkSpeed = 16;
Defaults to camera Field of View. Change this to your desired walk FOV.
config.WalkFOV = 70;
The amount in studs/sec to add to WalkSpeed after the multiplier. Defaults to 0.Ā
config.SprintSpeedModifier = 0;
Multiply WalkSpeed by this number. Defaults to 1.5.Ā
config.SprintSpeedMultiplier = 1.5;
The amount in degrees to add to WalkFOV. Defaults to 20.Ā
config.SprintFOVModifier = 20;
Action Keycode. Defaults to 'Left Shift'.Ā
config.SprintButton = Enum.KeyCode.LeftShift;
Work In Progress: Does nothing currently. Defaults to 'False'.Ā
config.TouchableButton = False;
Should sprint be toggled or held? Default False.Ā
config.ToggleSprint = False;
--[[ Sprint Script // Public Release 1.Y23W52.1
Copyright Brado Studios 2023
Last Update: 12.31.23
IMPORTANT: CLIENT ONLY MODULE. Running on server will result in errors.
Brado Studios is not responsible for any damages related to the use or modification of this script. Use/Modify at your own risk.
To use, require the module in a local script. To change the config, please look at the module documentation at "https://bradostudios.com"
"require(reference to module);"
]]
--SERVICES
local PLAYERSERVICE = game:GetService("Players");
local CONTEXTACTIONSERVICE = game:GetService("ContextActionService");
local RUNSERVICE = game:GetService("RunService");
local STARTERPLAYERSERVICE = game:GetService("StarterPlayer");
if RUNSERVICE:IsServer() then warn(`Attempted to call a client only module on the server. Please refer to module documentation for details.`); return {}; end;
--LOCAL VARIABLES
local player = PLAYERSERVICE.LocalPlayer;
local camera = workspace.CurrentCamera;
local character;
local isSprinting = false;
local button;
--CONFIG TABLE
local config = {}
config.walkSpeed = STARTERPLAYERSERVICE.CharacterWalkSpeed; --respects game's default walk speed
config.walkFOV = workspace.Camera.FieldOfView; --respects game's default FOV
config.SprintSpeedModifier = 0; --The amount of speed to add after the multiplyer in studs/sec
config.SprintSpeedMultiplyer = 1.5; --multiplies the walk speed
config.SprintFOVModifier = 20; --The amount of degrees to add to the walkFOV
config.SprintButton = Enum.KeyCode.LeftShift; --The key to press to sprint.
config.TouchableButton = false; --On screen button (WIP)
config.ToggleSprint = false; --If true, allows the player to press key to sprint instead of holding the key.
--FUNCTIONS
local function changeFOV(fovModifier) --modifies FOV. If called without arguments, falls back to defaults.
fovModifier = fovModifier or 0; --defaults to 0
camera.FieldOfView = config.walkFOV + fovModifier;
end;
local function changeWalkSpeed(speedMod, speedMulti) --modifies character walk speed. If called without arguments, falls back to defaults.
if not character then return; end; --If the character does not exist, skip the speed modification
speedMod = speedMod or 0; --defaults to 0
speedMulti = speedMulti or 1; --defaults to 1
character:WaitForChild("Humanoid").WalkSpeed = (config.walkSpeed * speedMulti) + speedMod;
end;
local function sprint() --groups all functions needed to start "sprinting"
changeWalkSpeed(config.SprintSpeedModifier, config.SprintSpeedMultiplyer);
changeFOV(config.SprintFOVModifier);
end;
local function walk() --groups all functions needed to stop "sprinting"
changeWalkSpeed();
changeFOV();
end;
--EVENT FUNCTIONS
local function sprintHandler(actionName, inputState, _inputObject) --When called by the event, checks if input is begining or ending, then calls the appropriateĀ
if inputState == Enum.UserInputState.Begin then --detects if input started. Special rules for config.ToggleSprint
if config.ToggleSprint and isSprinting then
isSprinting = false;
walk();
else
isSprinting = true;
sprint();
end
elseif inputState == Enum.UserInputState.End then --detects if input ended. If config.ToggleSprint, this part is ignored
if not config.ToggleSprint then
isSprinting = false;
walk();
end;
end;
end;
--EVENTS
CONTEXTACTIONSERVICE:BindAction("Sprint", sprintHandler, config.TouchableButton, config.SprintButton);
--CHARACTER HANDLER
player.CharacterAdded:Connect(function()
character = player.Character;
walk() --ensures the player is using defaults.
end);
player.CharacterRemoving:Connect(function()
character = nil;
end);
--RETURN MODULE
return config;