Hello devs! In this post you will know how to make a sprinting system when you click on a GUI! I should also mention that this is my first tutorial, so I’ll try my best to make you guys understand! This tutorial shouldn’t be that hard though!
Alright now let’s get started!
First create a new ScreenGui under StarterGui. Then create a TextButton / ImageButton and name it SprintButton. Now create a new LocalScript under the ScreenGui and type the following code;
-- Gui Variables --
local SprintButton = script.Parent.SprintButton -- Getting the Button
-- Player Variables --
local player = game.Players.LocalPlayer -- The player
local character = player.Character or player.CharacterAdded:Wait() -- The character of the player
local humanoid = character:WaitForChild("Humanoid") -- The humanoid of the character
-- Camera Variables --
local camera = game.Workspace.CurrentCamera -- Your current camera
local TweenService = game:GetService("TweenService") -- Getting the TweenService
-- Other Variables --
local debounce = false
local OnSprintingProperties = {FieldOfView = 100} -- Make sure it's the FieldOfView(FOV) when you're sprinting.
local NotSprintingProperies = {FieldOfView = 70} -- Default FieldOfView(FOV)
local info = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out) -- This part is the TweenInfo.
-- Scripting --
-- Let's get into the fun stuff!
local function OnSprintingSetUp() -- Creating a new function. This function sets up our camera and stuff when we're sprinting.
local tween = TweenService:Create(camera, info, OnSprintingProperties) -- We create the tween here.
tween:Play() -- Then we play the tween
end
local function NotSprintingSetUp() -- Creating a new function. This function sets up our camera and stuff when we're sprinting.
local tween = TweenService:Create(camera, info, NotSprintingProperies) -- Again creating the tween.
tween:Play() -- And playing it.
end
SprintButton.MouseButton1Click:Connect(function() -- When we click the SprintButton we do all the stuff down here.
if not debounce then -- If the debounce is set to false then
debounce = true -- Setting the debounce to true
OnSprintingSetUp() -- Setting up the OnSprintingSetUp() function
print("WalkSpeed: 20") -- Printing to test if it's working.
humanoid.WalkSpeed = 20 -- Changing the humanoid's WalkSpeed
wait(5) -- Waiting for a little bit.
NotSprintingSetUp() -- Then running the NotSprintingSetUp() function
print("WalkSpeed: 16") -- Printing again
humanoid.WalkSpeed = 16 -- Changing it back to default.
wait(2) -- We will not allow the player to sprint again for the next 2 seconds. You can change it to your own wish.
debounce = false -- And finally setting the debounce to false.
end
end)
When all done and you’re all set up, it should work just fine! Reply on this post if there’s any errors. I’ll be glad to answer them! See you all soon!
- From AridFights1 / Arid