Advertisement
munciseek

Untitled

Sep 20th, 2024 (edited)
1,381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.28 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local TweenService = game:GetService("TweenService")
  3.  
  4. local Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
  5.  
  6. -- 常量
  7. local WHITE = Color3.fromRGB(255, 255, 255)
  8. local BROWN = Color3.fromRGB(56, 46, 39)
  9. local BEIGE = Color3.fromRGB(213, 185, 158)
  10. local TRANSPARENT = 1
  11.  
  12. -- 创建 GUI 元素的函数
  13. local function createGuiElement(className, properties)
  14. local element = Instance.new(className)
  15. for property, value in pairs(properties) do
  16. element[property] = value
  17. end
  18. return element
  19. end
  20.  
  21. -- 创建 GUI 元素
  22. local StaminaGui = createGuiElement("ScreenGui", {Name = "StaminaGui", Parent = Parent, Enabled = true, ZIndexBehavior = Enum.ZIndexBehavior.Sibling})
  23. local Sprint = createGuiElement("Frame", {Name = "Sprint", Parent = StaminaGui, AnchorPoint = Vector2.new(0, 1), BackgroundColor3 = WHITE, BackgroundTransparency = TRANSPARENT, Position = UDim2.new(0.93, 0, 0.5, 0), Size = UDim2.new(0.056, 0, 0.076, 0), SizeConstraint = Enum.SizeConstraint.RelativeYY, ZIndex = 1005})
  24. local ImageLabel = createGuiElement("ImageLabel", {Parent = Sprint, BackgroundColor3 = BEIGE, Size = UDim2.new(1, 0, 1, 0), SizeConstraint = Enum.SizeConstraint.RelativeYY, Visible = false})
  25. local Bar = createGuiElement("Frame", {Name = "Bar", Parent = Sprint, AnchorPoint = Vector2.new(0, 0.5), BackgroundColor3 = BROWN, BackgroundTransparency = 0.7, Position = UDim2.new(-2.726, 0, 0.5, 0), Size = UDim2.new(5, 0, 0.3, 0), ZIndex = 0})
  26. local Fill = createGuiElement("Frame", {Name = "Fill", Parent = Bar, AnchorPoint = Vector2.new(0, 0.5), BackgroundColor3 = BEIGE, Position = UDim2.new(0, 0, 0.5, 0), Size = UDim2.new(1, 0, 1, 0), ZIndex = 2})
  27.  
  28. -- 添加奔跑按钮
  29. local RunButton = createGuiElement("TextButton", {
  30. Parent = StaminaGui,
  31. Text = "Sprint",
  32. Size = UDim2.new(0.1, 0, 0.1, 0),
  33. Position = UDim2.new(0.9, 0, 0.5, 0), -- 放在屏幕右边
  34. BackgroundColor3 = BEIGE,
  35. TextColor3 = BROWN,
  36. Font = Enum.Font.SourceSans,
  37. TextScaled = true
  38. })
  39. local RunButtonCorner = createGuiElement("UICorner", {CornerRadius = UDim.new(0.25, 0), Parent = RunButton})
  40.  
  41. -- 变量
  42. local Plr = Players.LocalPlayer
  43. local Char = Plr.Character or Plr.CharacterAdded:Wait()
  44. local Hum = Char:WaitForChild("Humanoid")
  45.  
  46. local stamina, staminaMax = 100, 100
  47. local cooldown = false
  48. local isSprinting = false
  49.  
  50. local ModuleScripts = {
  51. MainGame = require(Plr.PlayerGui.MainUI.Initiator.Main_Game),
  52. }
  53.  
  54. -- Hook metamethod
  55. local nIdx; nIdx = hookmetamethod(game, "__newindex", newcclosure(function(t, k, v)
  56. if k == "WalkSpeed" then
  57. if ModuleScripts.MainGame.chase then
  58. v = ModuleScripts.MainGame.crouching and 15 or 22
  59. elseif ModuleScripts.MainGame.crouching then
  60. v = 8
  61. else
  62. v = isSprinting and 20 or 12
  63. end
  64. end
  65. return nIdx(t, k, v)
  66. end))
  67.  
  68. local zerostamtween = TweenService:Create(ImageLabel, TweenInfo.new(12), {ImageTransparency = 0})
  69.  
  70. local function toggleSprint()
  71. if cooldown then return end -- 如果在冷却状态,直接返回
  72.  
  73. if isSprinting then
  74. -- 停止奔跑
  75. isSprinting = false
  76. Hum:SetAttribute("SpeedBoost", 0)
  77. Hum.WalkSpeed = 12
  78. TweenService:Create(ImageLabel, TweenInfo.new(1), {ImageTransparency = 1}):Play()
  79. else
  80. -- 开始奔跑
  81. isSprinting = true
  82. Hum:SetAttribute("SpeedBoost", 4)
  83. zerostamtween:Play()
  84. while isSprinting and stamina > 0 do
  85. Hum.WalkSpeed = 20
  86. stamina = math.max(stamina - 1, 0)
  87. Fill.Size = UDim2.new(1 * stamina / staminaMax, 1, 1, 0)
  88. task.wait(0.1)
  89. end
  90. zerostamtween:Pause()
  91. isSprinting = false
  92. Hum:SetAttribute("SpeedBoost", 0)
  93. Hum.WalkSpeed = 12
  94. TweenService:Create(ImageLabel, TweenInfo.new(1), {ImageTransparency = 1}):Play()
  95.  
  96. if stamina == 0 then
  97. -- 冷却
  98. ModuleScripts.MainGame.caption("You are tired...", true)
  99. local noStaminaSound = Instance.new("Sound", workspace)
  100. noStaminaSound.SoundId = "rbxassetid://8258601891"
  101. noStaminaSound.Volume = 0.8
  102. noStaminaSound.PlayOnRemove = true
  103. noStaminaSound:Destroy()
  104. cooldown = true
  105. TweenService:Create(ImageLabel, TweenInfo.new(0.3), {ImageTransparency = 0}):Play()
  106. wait(0.3)
  107. TweenService:Create(ImageLabel, TweenInfo.new(10), {ImageTransparency = 1}):Play()
  108. for i = 1, staminaMax, 1 do
  109. stamina = i
  110. Fill.Size = UDim2.new(1 * stamina / staminaMax, 1, 1, 0)
  111. task.wait(0.14)
  112. end
  113. cooldown = false
  114. else
  115. -- 补充
  116. cooldown = false
  117. TweenService:Create(ImageLabel, TweenInfo.new(1), {ImageTransparency = 1}):Play()
  118. while not isSprinting do
  119. stamina = math.min(stamina + 1, staminaMax)
  120. Fill.Size = UDim2.new(1 * stamina / staminaMax, 1, 1, 0)
  121. task.wait(0.14)
  122. end
  123. end
  124. end
  125. end
  126.  
  127. -- 绑定按钮点击事件
  128. RunButton.MouseButton1Click:Connect(toggleSprint)
  129.  
  130. Hum:SetAttribute("SpeedBoost", 0)
  131. Hum.WalkSpeed = 12
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement