I’ve got a tool in replicated storage called “Combat” there is a script that adds it to the player’s backpack.
Ignore the dummies script.
I have two scripts a client and a server script
the server script is in control of the damage
The client is in control of the animations
the character on the left is doing the attack animation and the character on the right is going the idle/blocking animation
My issue is that the scripts just aren’t working, and I don’t know why
Client
-- LocalScript inside StarterCharacterScripts
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = player.Backpack:WaitForChild("Combat") -- The tool in the player's backpack
local mouse = player:GetMouse()
local humanoid = character:WaitForChild("Humanoid")
local attackAnim = tool:WaitForChild("attack") -- The attack animation in the tool
local idleAnim = tool:WaitForChild("idle") -- The idle animation in the tool
local canAttack = true
local attackCooldown = 1 -- 1 second cooldown for attacks
local punchDamage = 5
local kickDamage = 10
-- Function to perform attack animation and send damage to the server
local function performAttack()
if canAttack then
canAttack = false
-- Play the attack animation
local animTrack = humanoid:LoadAnimation(attackAnim)
animTrack:Play()
-- Fire damage events at the right time using keyframes
animTrack.KeyframeReached:Connect(function(keyframeName)
if keyframeName == "RightPunch" then
-- Right Punch
game.ReplicatedStorage:WaitForChild("CombatEvent"):FireServer(mouse.Target, punchDamage)
elseif keyframeName == "LeftPunch" then
-- Left Punch
game.ReplicatedStorage:WaitForChild("CombatEvent"):FireServer(mouse.Target, punchDamage)
elseif keyframeName == "Kick" then
-- Kick
game.ReplicatedStorage:WaitForChild("CombatEvent"):FireServer(mouse.Target, kickDamage)
end
end)
-- Set cooldown for the attack
wait(attackCooldown)
canAttack = true
end
end
-- Handle the tool equipped event
tool.Equipped:Connect(function()
local idleAnimTrack = humanoid:LoadAnimation(idleAnim)
idleAnimTrack:Play()
end)
tool.Activated:Connect(function()
performAttack()
end)
Server
-- Script: Inside ServerScriptService
local combatEvent = game.ReplicatedStorage:WaitForChild("CombatEvent")
combatEvent.OnServerEvent:Connect(function(player, target, damage)
if target and target.Parent and target.Parent:FindFirstChild("Humanoid") then
local humanoid = target.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(damage)
end
end
end)