Remote event script doesn't work

I already posted about something similar to this today, but since i didn’t get this problem, so i’m posting this. Sorry about that.

So, we are making a JJBA game and there is stands, there is a “barrage” move which is fast punches. It needs to cancel after 2 seconds or when you stop pressing the key. It works, but when I do the thing in the video below, it doesnt stop.

Video:https://gyazo.com/e72d38e9112fc9500e9eee524b12aa3b

Local script of the move:

Mouse.KeyDown:Connect(function(Key)
if Key == "e" then
if Player.Character:FindFirstChild("IsMakingAMove").Value == false then
wait(0.1)
if Player.Character:FindFirstChild("IsMakingAMove").Value == false then
if BarrageCooldown == false then
if Summoned == true then
BarrageEvent:FireServer(Stand)
BarrageCooldown = true
IsBarraging = true
wait(2)
if IsBarraging == true then
CancelBarrageEvent:FireServer(Stand)
IsBarraging = false
end
BarrageCooldown = false
end
end
end
end
end
end)

Mouse.KeyUp:Connect(function(Key)
if Key == "e" then
if IsBarraging == true then
if Summoned == true then
CancelBarrageEvent:FireServer(Stand)
IsBarraging = false
end
end
end
end)

Server script:

game.ReplicatedStorage.Barrage.OnServerEvent:Connect(function(Player, TheStand)
local Character = Player.Character 
BarrageValue = true
Player.Character:FindFirstChild("IsMakingAMove").Value = true
local AnimController = TheStand.Controller
local BarrageAnim = AnimController:LoadAnimation(TheStand.Barrage)
BarrageAnim:Play()
ReplicatedStorage.Barrage:FireClient(Player)
function cancelBarrage()
BarrageAnim:Stop()
ReplicatedStorage.CancelBarrage:FireClient(Player)
wait(0.2)
Player.Character:FindFirstChild("IsMakingAMove").Value = false
BarrageValue = false
end
wait(2.2)
if BarrageValue == true then
Player.Character:FindFirstChild("IsMakingAMove").Value = false
BarrageValue = false
end
end)

game.ReplicatedStorage.CancelBarrage.OnServerEvent:Connect(function(Player, TheStand)
cancelBarrage()
end)

Sorry about the mess in script.

I tried my best to fix it myself but i can’t stand it anymore, so i decided to post it.

Hi!

First of all, you are trying to use the function below locally, which means it won’t work unless it is written for only global purposes. As long as you use it in another function, it won’t work.

Maybe try removing that function completely and defining the same things such as AnimController or BarrageAnim variables in the second function connected with OnServerEvent signal, like this.

game.ReplicatedStorage.CancelBarrage.OnServerEvent:Connect(function(Player, TheStand)
    local AnimController = TheStand.Controller
    local BarrageAnim = AnimController:LoadAnimation(TheStand.Barrage)

    BarrageAnim:Stop()
    ReplicatedStorage.CancelBarrage:FireClient(Player)
    wait(0.2)
    Player.Character:FindFirstChild("IsMakingAMove").Value = false
end)

And my advice is to use UserInputService since Mouse is superseded by that.

Well, my script was already like this, there was still the same error.