I’d like to detect and and continuously play my blocking animations while the player is holding down certain keys, but I can’t figure out how to continuously fire a remote event while somebody is holding down a key.
I have a script that plays single animations when a key is pressed already:
local RightPunchEvent = game.ReplicatedStorage:WaitForChild("RightPunch")
local LeftPunchEvent = game.ReplicatedStorage:WaitForChild("LeftPunch")
local RightBlockEvent = game.ReplicatedStorage:WaitForChild("RightBlock")
local LeftBlockEvent = game.ReplicatedStorage:WaitForChild("LeftBlock")
local function onKeyPress(input)
if input.KeyCode == Enum.KeyCode.E then
RightPunchEvent:FireServer()
elseif input.KeyCode == Enum.KeyCode.Q then
LeftPunchEvent:FireServer()
elseif input.KeyCode == Enum.KeyCode.U then
LeftBlockEvent:FireServer()
elseif input.KeyCode == Enum.KeyCode.J then
RightBlockEvent:FireServer()
end
end
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
In InputBegan, add a check for the blocking key and play the animation with that. Then, use InputEnded and stop the animation if Input.KeyCode is the same as the blocking one.
I wouldn’t recommend spamming remote events to hold block. Instead you can do the following:
Add another function of code which checks if the input ended and then sends it with the remote:
local function onInputEnded(input)
if input.KeyCode == Enum.KeyCode.E then
RightPunchEvent:FireServer("InputEnded")
elseif input.KeyCode == Enum.KeyCode.Q then
LeftPunchEvent:FireServer("InputEnded")
elseif input.KeyCode == Enum.KeyCode.U then
LeftBlockEvent:FireServer("InputEnded")
elseif input.KeyCode == Enum.KeyCode.J then
RightBlockEvent:FireServer("InputEnded")
end
end
game:GetService("UserInputService").InputEnded:Connect(onKeyPress)
Notice how I switched InputBegan to InputEnded and send a string (“InputEnded”)?
Now you want to receive the information on the server and check the information sent (you can also use a remote event for each step but I prefer this way):
...
...
...
RightPunchEvent.OnServerEvent:Connect(function(player, information)
if information == "InputEnded" then
-- stop the animation
else
-- start the animation
end
end)
You can also send an information when it should start the information to improve readability. Also be careful to add players as a value as it is automatically sending it and you want to avoid errors.
Im on my phone and cant see if I did grammar mistakes. Sorry in advance.