Repository navigation
Expand file tree
/
Copy pathRunSystemClient.luau
More file actions
55 lines (48 loc) · 1.83 KB
/
Copy pathRunSystemClient.luau
File metadata and controls
55 lines (48 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
--[[
RunSystemClient.lua
This script handles the running mechanic for players, consuming stamina when running and resetting speed when stamina is depleted.
]]
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Event = game.ReplicatedStorage:WaitForChild("RunEvent")
local UserInputService = game:GetService("UserInputService")
local canRun = true
local isRunning = false
local runLoop = nil
Event.OnClientEvent:Connect(function(stamina)
if stamina <= 0 then
canRun = false
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid.WalkSpeed = 16
end
print("Stamina depleted. Cannot run.")
else
canRun = true
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.LeftShift and canRun and not isRunning then
isRunning = true
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid.WalkSpeed = 32
end
Event:FireServer(true) -- Notify server running started
runLoop = task.spawn(function()
while isRunning and canRun do
Event:FireServer(true)
task.wait(0.1)
end
end)
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.LeftShift and isRunning then
isRunning = false
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid.WalkSpeed = 16
end
Event:FireServer(false) -- Notify server running stopped
end
end)