So I have a script. And it works just fine. until I put some code in it that I need.
This script handles attacks.
And whenever I use one attack, its also registering the other attack.
Even though it should not.
When I have it like that It works just fine!
if keysDownTable['X'] and keysDownTable['S'] then
print('Down attack')
attackEvent:FireServer('DownAttack')
elseif keysDownTable['X'] and keysDownTable['W'] then
print('Up attack, client')
elseif keysDownTable['X'] then -- this is where things go wrong.
print('Hard attack, client')
end
But once I change it to this
if keysDownTable['X'] and keysDownTable['S'] then
print('Down attack')
attackEvent:FireServer('DownAttack')
elseif keysDownTable['X'] and keysDownTable['W'] then
print('Up attack, client')
elseif keysDownTable['X'] then -- this is where things go wrong
print('Hard attack, client')
attackEvent:FireServer('ForwardAttack')
end
It just starts firing the attack anways.
I have no idea why this could be happening. I have checked all my other code and have narrowed it down so I know the problem is right here in this script.
Can anyone tell me why this is happening?
Full code
local uis = game:GetService('UserInputService')
local player = game:GetService('Players').LocalPlayer
local char = player.Character
wait(1)
local hum = char.Humanoid
local keysDownTable = {}
local function beginInput(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then -- this is a keyboard
print('SOMETHING PRESSED')
local keysPressed = uis:GetKeysPressed()
local keyCode = input.KeyCode
print(keyCode)
local attackEvent = game:GetService('ReplicatedStorage'):FindFirstChild('AttackEvents'):FindFirstChild('Attack')
-- check for certain inputs
if uis:IsKeyDown(Enum.KeyCode.X) and uis:IsKeyDown(Enum.KeyCode.W) then
print('Keys X and W pressed')
keysDownTable['W'] = Enum.KeyCode.W
keysDownTable['X'] = Enum.KeyCode.X
elseif uis:IsKeyDown(Enum.KeyCode.X) and uis:IsKeyDown(Enum.KeyCode.S) then
keysDownTable['S'] = Enum.KeyCode.S
keysDownTable['X'] = Enum.KeyCode.X
elseif uis:IsKeyDown(Enum.KeyCode.X) then
keysDownTable['X'] = Enum.KeyCode.X
elseif uis:IsKeyDown(Enum.KeyCode.G) then
elseif uis:IsKeyDown(Enum.KeyCode.Z) then
elseif uis:IsKeyDown(Enum.KeyCode.R) then
end
-- now check for which inputs were detected
print(keysDownTable) -- this has all the keys they pressed
if keysDownTable['X'] and keysDownTable['S'] then
print('Down attack')
attackEvent:FireServer('DownAttack')
elseif keysDownTable['X'] and keysDownTable['W'] then
print('Up attack, client')
elseif keysDownTable['X'] then
print('Hard attack, client')
attackEvent:FireServer('ForwardAttack')
end
-- remove items
for n, item in pairs(keysDownTable) do
print(n,item)
keysDownTable[n] = nil
print('Removing item' .. n,item)
end
-- items have been removed
end
end
end
local function endInput(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.Keyboard then
local keyCode = input.KeyCode
end
end
end
uis.InputBegan:Connect(beginInput)
uis.InputEnded:Connect(endInput)