Greetings Dev Forum. I have a rather simple issue with my punching script. I want to utilize an additional punch animation for the left arm which plays after the right arm punch animation. Any help is appreciated!
--//This is in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Root = character:WaitForChild("HumanoidRootPart")
local Animation = character:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("Animation"))
--// Settings
--//local Keybind = UserInputType.MouseBot
local Damage = 5
local Debounce = 0.1
local Keybind = Enum.KeyCode.F
local CanPunch = true
UserInputService.InputBegan:Connect(function(input,busy)
if input.KeyCode == Keybind and not busy then
if CanPunch == true then
CanPunch = false
Animation:Play()
Animation.Looped = false
game.ReplicatedStorage.Remote.Punch:FireServer(Damage)
wait(Debounce)
CanPunch = true
end
end
end)
--// This is in ServerScriptService
game.ReplicatedStorage.Remote.Punch.OnServerEvent:Connect(function(player, damage)
for i,target in pairs(game.Workspace:GetDescendants()) do
if target.Name ~= player.Name then
if(target.Character.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < 5 then
target.Character.Humanoid.Health -= damage
end
end
end
end)
--// ~= is not equal to
You can use the modulus ‘%’ operator and a counting system to consecutively load animations from a table like so:
--//This is in StarterCharacterScripts
local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Root = character:WaitForChild("HumanoidRootPart")
--// Settings
--//local Keybind = UserInputType.MouseBot
local count = 1
local Anims = {
script:WaitForChild('Animation1'), --change these to
script:WaitForChild('Animation2'), --your animations
} --you can add more and it will still work
local Damage = 5
local Debounce = 0.1
local Keybind = Enum.KeyCode.F
local CanPunch = true
UserInputService.InputBegan:Connect(function(input,busy)
if input.KeyCode == Keybind and not busy then
if CanPunch == true then
CanPunch = false
count = (count%#Anims)+1 --this is gonna return the index of the next animation in 'Anims'
--load the animation from Anims table at index 'count' position
local Animation = character:WaitForChild("Humanoid"):LoadAnimation(Anims[count])
Animation:Play()
Animation.Looped = false
game.ReplicatedStorage.Remote.Punch:FireServer(Damage)
wait(Debounce)
CanPunch = true
end
end
end)
Basically what it does is load the next animation in the table by increasing its index by 1. If it reaches the end of the table it goes back to the first animation in the table due to the % operator