Hello! I would like to know how I could improve my combat system code
I have a combat system code implemented and I would like to receive tips and help to improve it, could you give me your feedback, please! Thank you very much
Localscript
-- Declared
local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local stopAnim = player.Character
local mouse = player:GetMouse()
-- Defense animation
local defanim = Instance.new("Animation")
defanim.AnimationId = "rbxassetid://6229874629"
-- Light hit animation
local light_hit_anim = Instance.new("Animation")
light_hit_anim.AnimationId = "rbxassetid://6232686373"
-- Concentrate hit animation
local con_hit_anim = Instance.new("Animation")
con_hit_anim.AnimationId = "rbxassetid://6232702541"
-- Input system
UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
-- Defense anim F key
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.F then
print("Playing defense animation! done.")
local playAnim = humanoid:LoadAnimation(defanim)
playAnim:Play()
script.DefAnim:FireServer()
end
end)
-- Light attack anim with the left mouse click:
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
print("LMB pressed, playing the anim")
local playAnim = humanoid:LoadAnimation(light_hit_anim)
playAnim:Play()
script.LightHitAnim:FireServer()
end
end)
-- Concentrate attack anim with the right mouse click:
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
if input.UserInputType == Enum.UserInputType.MouseButton2 then
print("LMB pressed, playing the anim")
local playAnim = humanoid:LoadAnimation(con_hit_anim)
playAnim:Play()
script.BigHitAnim:FireServer()
end
end) -- THAT ARE EVENTS
ServerScript
-- SERVER CONECCTION
-- Defense anim connected to server
script.Parent.Animations.DefAnim.OnServerEvent:Connect(function()
print("Connected to defense anim.")
end)
-- Light hit anim connected to server
script.Parent.Animations.LightHitAnim.OnServerEvent:Connect(function(plr)
print("Connected to light hit anim.")
-- Damage functions
local AttackDist = 10 --If a player is further than this value, they won't be damaged.
for i,v in pairs(game.Players:GetChildren()) do --Loops through all players.
if v.Character ~= nil and plr.Character ~= nil and v ~= plr then --Checks if there's a character
local pRoot = plr.Character:FindFirstChild("HumanoidRootPart")
local vRoot = v.Character:FindFirstChild("HumanoidRootPart")
if pRoot and vRoot and (plr.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude < AttackDist then --Checks if the Root parts exist and if the player is in range.
local Hum = v.Character:FindFirstChild("Humanoid")
if Hum and Hum.Health > 0 then --Checks if player is alive.
Hum.Health = Hum.Health - 10 --Change "100" to the damage the player should take.
end
--Stuff that happens when a player is in range.
end
end
end
end)
-- Concentrate big hit anim connected to server
script.Parent.Animations.BigHitAnim.OnServerEvent:Connect(function(plr)
print("Connected to concentrate hit anim.")
-- Damage functions
local AttackDist = 10 --If a player is further than this value, they won't be damaged.
for i,v in pairs(game.Players:GetChildren()) do --Loops through all players.
if v.Character ~= nil and plr.Character ~= nil and v ~= plr then --Checks if there's a character
local pRoot = plr.Character:FindFirstChild("HumanoidRootPart")
local vRoot = v.Character:FindFirstChild("HumanoidRootPart")
if pRoot and vRoot and (plr.Character.HumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude < AttackDist then --Checks if the Root parts exist and if the player is in range.
local Hum = v.Character:FindFirstChild("Humanoid")
if Hum and Hum.Health > 0 then --Checks if player is alive.
Hum.Health = Hum.Health - 25 --Change "100" to the damage the player should take.
end
--Stuff that happens when a player is in range.
end
end
end
end)