Repository navigation
Expand file tree
/
Copy pathRunSystemServer.luau
More file actions
67 lines (53 loc) · 1.92 KB
/
Copy pathRunSystemServer.luau
File metadata and controls
67 lines (53 loc) · 1.92 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
56
57
58
59
60
61
62
63
64
65
66
67
--[[
RunSystemServer.lua
This script manages the running mechanic on the server side, handling stamina consumption and regeneration for players.
]]
local Players = game:GetService("Players")
local Event = Instance.new("RemoteEvent")
Event.Name = "RunEvent"
Event.Parent = game.ReplicatedStorage
-- Table to track if a player is running
local runningPlayers = {}
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local stamina = Instance.new("IntValue")
stamina.Name = "Stamina"
stamina.Value = 100
stamina.Parent = leaderstats
runningPlayers[player] = false
-- Regenerate stamina over time only if not running
task.spawn(function()
while player and player.Parent do
if stamina.Value < 100 and not runningPlayers[player] then
stamina.Value += 1
end
task.wait(0.05)
end
end)
player.AncestryChanged:Connect(function(_, parent)
if not parent then
runningPlayers[player] = nil
end
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)
Event.OnServerEvent:Connect(function(player, isRunning)
local stamina = player:WaitForChild("leaderstats"):WaitForChild("Stamina")
-- Update running state
if typeof(isRunning) == "boolean" then
runningPlayers[player] = isRunning
end
if runningPlayers[player] then
if stamina.Value > 0 then
stamina.Value -= 1
print(player.Name .. " is running. Stamina left: " .. stamina.Value)
else
print(player.Name .. " has no stamina left to run.")
runningPlayers[player] = false -- Stop running if stamina is depleted
isRunning = false -- Ensure running state is updated
end
end
Event:FireClient(player, stamina.Value) -- Notify the client about the stamina change
end)