我正在努力让玩家在站立不动时恢复体力的速度比行走时更快。到目前为止,耐力条只上升而不下降。我之前可以使用它,但只能使用 W 键,现在我正在尝试让它检测 WASD。
local player = game.Players.LocalPlayer
local char = player.Character
local UIS = game:GetService("UserInputService")
local stamina = 250
local isRunning = false
local isMoving = false
local isMovingW = false
local isMovingA = false
local isMovingS = false
local isMovingD = false
stamina = math.clamp(stamina, 0, 250)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
isMovingW = true
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
isMovingA = true
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.S then
isMovingS = true
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.D then
isMovingD = true
end
end)
if isMovingW == true or isMovingA == true or isMovingS == true or isMovingD == true then
isMoving = true
end
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
isMovingW = false
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.A then
isMovingA = false
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.S then
isMovingS = false
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.D then
isMovingD = false
end
end)
if isMovingW == false and isMovingA == false and isMovingS == false and isMovingD == false then
isMoving = false
end
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
isRunning = true
char.Humanoid.WalkSpeed = 32
while stamina > 0 and isRunning do
stamina = stamina -1
script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
wait()
if stamina <= 0 then
char.Humanoid.WalkSpeed = 16
end
end
end
end)
UIS.InputEnded:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then
isRunning = false
char.Humanoid.WalkSpeed = 16
while stamina < 250 and isMoving == true and not isRunning do
stamina = stamina +0.5
script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
wait()
while stamina < 250 and isMoving == false and not isRunning do
stamina = stamina +1
script.Parent:TweenSize(UDim2.new(stamina/250, 0, 1, 0), "Out", "Linear",0)
wait()
if stamina == 0 then
char.Humanoid.Walkspeed = 16
end
end
end
end
end)
这肯定不是最好的解决方案,但您可以检查玩家位置的变化而不是检查 WASD。
这样的东西应该可以工作(如果您决定使用它,请删除旧的运动检测):
--put this at the beginning of the script
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local root = char:FindFirstChild("HumanoidRootPart")
--put this after the rest of your code
while true do
pos1 = root.Position
wait(0.001) --you can lower this if you want it to be more accurate, but it will cause intense lag
pos2 = root.Position
if pos1 ~= pos2 then
isMoving = true
end
end