I'm making a game where one of the main mechanics is sword fighting. When you move left, you swing right and when you move left you swing right and also when you dont move left or right you swing down. Problem is, when you move left and right too fast the variable gets tripped up and it detects you as not moving. Is there a way to fix this or a better way to write this code because I did this without a tutorial.
#Sword direction problem
1 messages · Page 1 of 1 (latest)
rude
@formal crane youre trying to store
** You are now Level 14! **
the movement keys in a single varible
but as you saw, the inputs happen to fast it slips up
when you let go of one key, it just sets the varible to none even if you are holding the other key
local UserInputService = game:GetService("UserInputService")
local player = script.Parent
local humanoid = player:WaitForChild("Humanoid")
local isSwinging = false
local holdingA = false
local holdingD = false
local function getWalkingDirection()
if holdingA and not holdingD then
return "left"
elseif holdingD and not holdingA then
return "right"
else
return "none"
end
end
local function swordSwing()
local walkingDirection = getWalkingDirection()
if walkingDirection == "right" then
print("LeftSlash")
elseif walkingDirection == "left" then
print("RightSlash")
else
print("DownSlash")
end
end
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.A then
holdingA = true
elseif input.KeyCode == Enum.KeyCode.D then
holdingD = true
elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not isSwinging then
isSwinging = true
swordSwing()
task.wait(0.2)
isSwinging = false
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessed)
if input.KeyCode == Enum.KeyCode.A then
holdingA = false
elseif input.KeyCode == Enum.KeyCode.D then
holdingD = false
end
end)
try something like this
also
if youre making this for ONLY pc thats great
but if you want mobile support or concsole
i would recommend switching to
Humanoid.MoveDirection
freaking thank you