Hey Everyone,
I made a simple sprinting system with stamina yesterday, but I wasn’t satisfied because I know it can be exploited.
Here is the code:
local userInputService = game:GetService(“UserInputService”)
local runService = game:GetService(“RunService”)
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local staminaGui = playerGui:WaitForChild(“StaminaGui”)
local mainFrame = staminaGui:WaitForChild(“MainFrame”)
repeat wait() until player.Character
local character = player.Character
local speedModifier = 2
local sprintCost = 0.2
local running = false
local Stamina = 100
local currentStamina = 100
local oldWalkSpeed = {}
local debounce = false
spawn(function()
while wait() do
mainFrame.Status.Text = math.floor(currentStamina)…" / "…Stamina
end
end)
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
if debounce == false then
debounce = true
if currentStamina > sprintCost and character.Humanoid:GetState() == Enum.HumanoidStateType.RunningNoPhysics and character.Humanoid.MoveDirection.Magnitude > 0 then
running = true
oldWalkSpeed[player.Name] = character.Humanoid.WalkSpeed
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed * speedModifier
for i = 70, 75, 1 do
if running == true then
game.Workspace.Camera.FieldOfView = i
wait()
else
break
end
if i == 75 then
break
end
end
end
wait(1.5)
debounce = false
end
end
end)
userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.LeftShift then
if running == true then
running = false
character.Humanoid.WalkSpeed = oldWalkSpeed[player.Name]
oldWalkSpeed[player.Name] = nil
for i = 75, 70, -1 do
if running == false then
game.Workspace.Camera.FieldOfView = i
wait()
else
break
end
if i == 70 then
break
end
end
end
end
end)
runService.Heartbeat:Connect(function()
if running == true then
if currentStamina >= sprintCost then
currentStamina = currentStamina - (sprintCost)
mainFrame.Bar.Size = UDim2.new( (currentStamina / Stamina),0,1,0)
else
if currentStamina <= sprintCost or currentStamina == 0 then
currentStamina = 0
mainFrame.Bar.Size = UDim2.new( (currentStamina / Stamina),0,1,0)
character.Humanoid.WalkSpeed = oldWalkSpeed[player.Name]
oldWalkSpeed[player.Name] = nil
running = false
for i = 75, 70, -1 do
if running == false then
game.Workspace.Camera.FieldOfView = i
wait()
else
break
end
if i == 70 then
break
end
end
end
end
end
if running == false then
wait(1)
currentStamina = currentStamina + (sprintCost / 2)
mainFrame.Bar.Size = UDim2.new( (currentStamina / Stamina),0,1,0)
if currentStamina >= Stamina then
currentStamina = Stamina
mainFrame.Bar.Size = UDim2.new( (currentStamina / Stamina),0,1,0)
end
end
end)
My Main Questions
- Can this be exploited?
- If yes, then how can a hacker hack it and how can I fix it?
I am fairly new to Lua so I’m trying to learn the basics, please keep that in mind.
Thanks in advance