-- 跑步系统脚本 local Players = game:GetService("Players") local UserInputService = game:GetService("UserInputService") local RunService = game:GetService("RunService") -- 常量定义 local WALK_SPEED = 12 local RUN_SPEED = 30 local MAX_STAMINA = 100 local STAMINA_DRAIN_RATE = 6 -- 每秒消耗 local STAMINA_REGEN_RATE = 5 -- 每秒恢复 local STAMINA_RECOVERY_DELAY = 4 -- 体力耗尽后恢复延迟 local MIN_STAMINA_TO_RUN = 0 -- 修改:体力必须完全耗尽才停止(0%) -- 动画ID local RUN_ANIMATION_ID = "252557606" local WALK_ANIMATION_ID = "183294396" local IDLE_ANIMATION_ID = "183294396" -- 闲置动画ID -- 为每个玩家创建系统 local function createStaminaSystem(player) -- 等待玩家角色生成 local character = player.Character or player.CharacterAdded:Wait() local humanoid = character:WaitForChild("Humanoid") -- 创建玩家GUI local playerGui = player:WaitForChild("PlayerGui") local screenGui = Instance.new("ScreenGui") screenGui.Name = "StaminaSystem" screenGui.ResetOnSpawn = false screenGui.ZIndexBehavior = Enum.ZIndexBehavior.Sibling screenGui.Parent = playerGui -- 创建跑步按钮(更大的按钮) local runButton = Instance.new("ImageButton") runButton.Name = "RunButton" runButton.Size = UDim2.new(0, 120, 0, 120) -- 更大的按钮尺寸 runButton.Position = UDim2.new(1, -130, 1, -130) -- 右下角,调整位置适应更大尺寸 runButton.AnchorPoint = Vector2.new(1, 1) runButton.BackgroundTransparency = 1 runButton.Image = "rbxassetid://95196971913205" -- 初始为走路状态贴图 runButton.ScaleType = Enum.ScaleType.Fit -- 确保贴图正确显示 runButton.Parent = screenGui -- 添加按钮下方的Q字母 local qLabel = Instance.new("TextLabel") qLabel.Name = "QKeyLabel" qLabel.Size = UDim2.new(1, 0, 0, 25) -- 更大的文字 qLabel.Position = UDim2.new(0, 0, 1, 8) qLabel.BackgroundTransparency = 1 qLabel.Text = "Q" qLabel.TextColor3 = Color3.new(1, 1, 1) qLabel.TextScaled = true qLabel.Font = Enum.Font.GothamBold qLabel.Parent = runButton -- 创建体力条(固定在左下角) local staminaFrame = Instance.new("Frame") staminaFrame.Name = "StaminaFrame" staminaFrame.Size = UDim2.new(0, 200, 0, 25) staminaFrame.Position = UDim2.new(0, 20, 1, -50) -- 左下角 staminaFrame.AnchorPoint = Vector2.new(0, 1) staminaFrame.BackgroundColor3 = Color3.new(0, 0, 0) staminaFrame.BorderSizePixel = 2 staminaFrame.BorderColor3 = Color3.new(1, 1, 1) staminaFrame.Parent = screenGui local staminaBar = Instance.new("Frame") staminaBar.Name = "StaminaBar" staminaBar.Size = UDim2.new(1, 0, 1, 0) staminaBar.BackgroundColor3 = Color3.new(1, 1, 1) staminaBar.BorderSizePixel = 0 staminaBar.Parent = staminaFrame local staminaText = Instance.new("TextLabel") staminaText.Name = "StaminaText" staminaText.Size = UDim2.new(1, 0, 1, 0) staminaText.BackgroundTransparency = 1 staminaText.Text = "100/100" staminaText.TextColor3 = Color3.new(0, 0, 0) staminaText.TextScaled = true staminaText.Font = Enum.Font.GothamBold staminaText.Parent = staminaFrame -- 玩家状态变量 local isRunning = false local wantsToRun = false -- 新增:玩家是否想要跑步 local currentStamina = MAX_STAMINA local canRegenStamina = true local staminaDepleted = false local recoveryDelayEndTime = 0 -- 动画相关变量 local runAnimation local walkAnimation local idleAnimation local currentAnimationTrack local animationsLoaded = false -- 加载动画 local function loadAnimations() if character and humanoid then -- 创建动画对象 runAnimation = Instance.new("Animation") runAnimation.AnimationId = "rbxassetid://" .. RUN_ANIMATION_ID walkAnimation = Instance.new("Animation") walkAnimation.AnimationId = "rbxassetid://" .. WALK_ANIMATION_ID idleAnimation = Instance.new("Animation") idleAnimation.AnimationId = "rbxassetid://" .. IDLE_ANIMATION_ID animationsLoaded = true print("Animations loaded for player: " .. player.Name) end end -- 播放移动动画 local function playMovementAnimation() if not animationsLoaded or not humanoid then return end -- 停止当前动画 if currentAnimationTrack then currentAnimationTrack:Stop() currentAnimationTrack = nil end -- 根据状态播放对应动画 local animationToPlay = walkAnimation if isRunning then animationToPlay = runAnimation end currentAnimationTrack = humanoid:LoadAnimation(animationToPlay) if currentAnimationTrack then currentAnimationTrack:Play() end end -- 播放闲置动画 local function playIdleAnimation() if not animationsLoaded or not humanoid then return end -- 停止当前动画 if currentAnimationTrack then currentAnimationTrack:Stop() currentAnimationTrack = nil end -- 播放闲置动画 currentAnimationTrack = humanoid:LoadAnimation(idleAnimation) if currentAnimationTrack then currentAnimationTrack:Play() end end -- 停止移动动画 local function stopMovementAnimation() if currentAnimationTrack then currentAnimationTrack:Stop() currentAnimationTrack = nil end end -- 检查玩家是否在移动 local function isPlayerMoving() local rootPart = character:FindFirstChild("HumanoidRootPart") if not rootPart then return false end return humanoid.MoveDirection.Magnitude > 0.1 end -- 更新体力条显示 local function updateStaminaDisplay() local staminaPercent = currentStamina / MAX_STAMINA staminaBar.Size = UDim2.new(staminaPercent, 0, 1, 0) staminaText.Text = math.floor(currentStamina) .. "/" .. MAX_STAMINA -- 体力低时改变颜色 if currentStamina < 10 then staminaBar.BackgroundColor3 = Color3.new(1, 0, 0) -- 红色(极低) elseif currentStamina < 30 then staminaBar.BackgroundColor3 = Color3.new(1, 0.5, 0) -- 橙色(低) elseif currentStamina < 60 then staminaBar.BackgroundColor3 = Color3.new(1, 1, 0) -- 黄色(中等) else staminaBar.BackgroundColor3 = Color3.new(1, 1, 1) -- 白色(充足) end end -- 检查是否可以跑步 local function canRun() return not staminaDepleted and currentStamina > MIN_STAMINA_TO_RUN end -- 强制停止跑步(体力耗尽时调用) local function forceStopRunning() if isRunning then isRunning = false humanoid.WalkSpeed = WALK_SPEED runButton.Image = "rbxassetid://95196971913205" -- 走路状态贴图 -- 如果正在移动,更新动画 if isPlayerMoving() then playMovementAnimation() else playIdleAnimation() -- 停止移动时播放闲置动画 end end end -- 更新跑步状态(根据玩家意愿和体力条件) local function updateRunState() if wantsToRun and canRun() then -- 玩家想要跑步且体力允许 if not isRunning then isRunning = true humanoid.WalkSpeed = RUN_SPEED runButton.Image = "rbxassetid://98026253832310" -- 跑步状态贴图 -- 如果正在移动,播放跑步动画 if isPlayerMoving() then playMovementAnimation() end end else -- 玩家不想跑步或体力不允许 if isRunning then isRunning = false humanoid.WalkSpeed = WALK_SPEED runButton.Image = "rbxassetid://95196971913205" -- 走路状态贴图 -- 如果正在移动,播放走路动画,否则播放闲置动画 if isPlayerMoving() then playMovementAnimation() else playIdleAnimation() end end end end -- 切换跑步意愿 local function toggleRunDesire() wantsToRun = not wantsToRun updateRunState() end -- 体力系统更新 local function updateStamina(deltaTime) local moving = isPlayerMoving() -- 检查恢复延迟 if not canRegenStamina and tick() >= recoveryDelayEndTime then canRegenStamina = true staminaDepleted = false print("Stamina recovery delay ended") end if isRunning then if moving then -- 跑步时移动:消耗体力 currentStamina = math.max(0, currentStamina - STAMINA_DRAIN_RATE * deltaTime) if currentStamina <= 0 then currentStamina = 0 staminaDepleted = true -- 体力耗尽时强制停止跑步 forceStopRunning() -- 启动恢复延迟 canRegenStamina = false recoveryDelayEndTime = tick() + STAMINA_RECOVERY_DELAY print("Stamina depleted, recovery delay started") end else -- 跑步时不移动:恢复体力 if canRegenStamina then currentStamina = math.min(MAX_STAMINA, currentStamina + STAMINA_REGEN_RATE * deltaTime) end end else -- 走路状态:恢复体力 if canRegenStamina then currentStamina = math.min(MAX_STAMINA, currentStamina + STAMINA_REGEN_RATE * deltaTime) -- 体力恢复到0%以上时,解除跑步限制 if currentStamina > MIN_STAMINA_TO_RUN and staminaDepleted then staminaDepleted = false print("Stamina no longer depleted") -- 体力恢复后,如果玩家还想跑步,自动切换到跑步状态 updateRunState() end end end updateStaminaDisplay() end -- 处理移动状态变化 local function handleMovementChange() if not animationsLoaded then return end local moving = isPlayerMoving() if moving then -- 开始移动,播放对应动画 playMovementAnimation() else -- 停止移动,播放闲置动画 playIdleAnimation() end end -- 按钮点击事件 runButton.MouseButton1Click:Connect(function() toggleRunDesire() end) -- 键盘Q键事件 local function onInput(input, gameProcessed) if gameProcessed then return end if input.KeyCode == Enum.KeyCode.Q then toggleRunDesire() end end UserInputService.InputBegan:Connect(onInput) -- 角色死亡重置 local function onCharacterAdded(newCharacter) character = newCharacter humanoid = character:WaitForChild("Humanoid") -- 重置状态 isRunning = false wantsToRun = false currentStamina = MAX_STAMINA staminaDepleted = false canRegenStamina = true humanoid.WalkSpeed = WALK_SPEED runButton.Image = "rbxassetid://95196971913205" -- 初始使用走路贴图 -- 重置动画 animationsLoaded = false currentAnimationTrack = nil runAnimation = nil walkAnimation = nil idleAnimation = nil -- 延迟加载动画 delay(0.5, function() loadAnimations() -- 初始状态播放闲置动画 if animationsLoaded then playIdleAnimation() end end) updateStaminaDisplay() print("Character reset for player: " .. player.Name) end player.CharacterAdded:Connect(onCharacterAdded) -- 主更新循环 local connection local lastMovingState = false connection = RunService.Heartbeat:Connect(function(deltaTime) if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health > 0 then -- 更新体力系统 updateStamina(deltaTime) -- 检查移动状态变化 local currentMovingState = isPlayerMoving() if currentMovingState ~= lastMovingState then handleMovementChange() lastMovingState = currentMovingState end end end) -- 玩家离开时清理 player.AncestryChanged:Connect(function() if not player:IsDescendantOf(Players) then connection:Disconnect() end end) -- 初始化显示和动画 updateStaminaDisplay() -- 延迟加载动画并播放初始闲置动画 delay(1, function() loadAnimations() if animationsLoaded then playIdleAnimation() end end) end -- 为所有玩家创建系统 Players.PlayerAdded:Connect(createStaminaSystem) -- 为已存在的玩家创建系统 for _, player in ipairs(Players:GetPlayers()) do createStaminaSystem(player) end