I have a game when holding f does a block. However, when i tap f fast, it toggles on. It seems that InputEnded does not register when I tap it.
Here is my code:
UIS.InputBegan:Connect(function(key,typing)
if typing then return end
if Attacking or Stunned or IsRagdoll or Blocking then return end
if key.KeyCode == Enum.KeyCode.F and DB==false and game:GetService("Players").LocalPlayer.Character:GetAttribute("Equipped")==true and plr.Character:GetAttribute("CanDefend") then
animationsModule.Blocking(char)
DB=true
startBlocking()
task.wait(0.3)
DB = false
end
end)
UIS.InputEnded:Connect(function(key,typing)
if key.KeyCode == Enum.KeyCode.F and not typing and not Stunned and char:GetAttribute(“Blocking”) then
print(“Stop Blocking”)
stopBlocking()
animationsModule.UnBlocking(char)
–char.Humanoid.Animator:LoadAnimation(RS:WaitForChild(“Animations”):WaitForChild(“Sword”):WaitForChild(“Idle”)):Play()
My guess is because you have a task.wait() which yields code until it’s finished. You could replace it with task.delay() and put the code you want to run after the wait in a function like this:
Thanks for the reply, but I don’t think it worked. As you can see in this video:
sometimes it goes down as usual but sometimes I still stay blocking. I have no clue why.
Also, it doesn’t make sense but I feel like the “toggle” bug happens less often then before even though it didn’t fix the problem. Strange
Here is my new code for reference:
UIS.InputBegan:Connect(function(key,typing)
if typing then return end
if Attacking or Stunned or IsRagdoll or Blocking then return end
if key.KeyCode == Enum.KeyCode.F and DB==false and game:GetService("Players").LocalPlayer.Character:GetAttribute("Equipped")==true and plr.Character:GetAttribute("CanDefend") then
animationsModule.Blocking(char)
DB=true
startBlocking()
task.delay(0.3, function()
DB = false
end)
end
end)
UIS.InputEnded:Connect(function(key,typing)
if key.KeyCode == Enum.KeyCode.F and not typing and not Stunned and char:GetAttribute("Blocking") then
print("Stop Blocking")
stopBlocking()
animationsModule.UnBlocking(char)
--char.Humanoid.Animator:LoadAnimation(RS:WaitForChild("Animations"):WaitForChild("Sword"):WaitForChild("Idle")):Play()
end
end)
I didn’t understand what’s happening in the video, maybe because I don’t know when you’re pressing block but I do notice that the output prints “Stop blocking” everytime it looks like you stop blocking. I also wonder what is happening in the other functions and module script that your code references and maybe it could be a problem with the characters attributes. (I guess what I’m saying is I don’t really understand your problem now)
so Every time I block, I am pressing f. On my IRL keyboard, I am tapping f. When I tap f but press it deeply for a while, it works fine. However, you can see that sometimes in the video, I don’t stop blocking for a while. That is when I tapped it fast. I tapped F on my keyboard quickly but it didn’t register to stop blocking.
local function startBlocking()
blockingEvent:FireServer("Start")
end
local function stopBlocking()
blockingEvent:FireServer("Stop")
for i,v in pairs(char:WaitForChild("Humanoid"):GetPlayingAnimationTracks()) do
if v.Name == "Idle" and v.Name=="Walk" then return end
v:Stop()
end
end
Server Code:
local RS = game:GetService("ReplicatedStorage")
local blockingEvent = RS:WaitForChild("Events"):WaitForChild("Blocking")
local Debris = game:GetService("Debris")
local Parryframes = 0.45
blockingEvent.OnServerEvent:Connect(function(plr,action)
local CurrentTime = tick()
if action == "Start" then
plr.Character:SetAttribute("Blocking",true)
--SFX
local blockSFX = game.SoundService.SFX.Sword:WaitForChild("BlockStart"):Clone()
blockSFX.Parent = plr.Character:WaitForChild("HumanoidRootPart")
blockSFX:Play()
Debris:AddItem(blockSFX,1.1)
--
plr.Character:SetAttribute("Blocking",true)
plr.Character:FindFirstChild("Humanoid").WalkSpeed=10
task.spawn(function()
plr.Character:SetAttribute("Parrying",true)
task.wait(Parryframes)
plr.Character:SetAttribute("Parrying",false)
end)
end
if action == "Stop" then
plr.Character:SetAttribute("Blocking",false)
plr.Character:SetAttribute("Parrying",false)
task.delay(Parryframes,function()
plr.Character:SetAttribute("Parrying",false)
end)
plr.Character:SetAttribute("LastBlockingStopTime",CurrentTime)
plr.Character:FindFirstChild("Humanoid").WalkSpeed = 16
end
end)
while true do
task.wait(1)
for _, player in pairs(game.Players:GetPlayers()) do
local char = player.Character
if char then
local isBlocking = char:GetAttribute("Blocking")
local LastStopTime = char:GetAttribute("LastBlockingStopTime") or 0
local currentTime = tick()
if not isBlocking and currentTime-LastStopTime >= 3 then
char:SetAttribute("Posture",math.max(0,char:GetAttribute("Posture")-1))
if char:GetAttribute("Posture") < 0 then
char:SetAttribute("Posture",0)
end
end
end
end
end
I can’t say exactly what the issue is, but there might be some delay before the server finishes all it’s code when you activate the block. This might be a problem because of you stop pressing f quickly before all the attributes are set or the server side code completes, then your code might not get past the if statement in the input ended function. I’d look into how quickly the server code finishes vs how fast the input ended runs to see if that’s the issue.