Hello everyone. There is this weird bug where my combat system only detects hits every other time. Do you guys know a fix for this?
For some reason, the hit is only happening every other time. Does anyone know a fix for this?
Server Code:
local Debris = game:GetService("Debris")
local SS = game:GetService("SoundService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")
local Tool = script.Parent
local animationIds = {
["Stab1"] = "rbxassetid://18894699991",
["Stab2"] = "rbxassetid://18894701886",
["Stab3"] = "rbxassetid://18894703743",
["Stab4"] = "rbxassetid://18894705958",
}
local currentAnimation = "Stab1"
local function GetNextAnimation(current)
local animations = {"Stab1", "Stab2", "Stab3", "Stab4"}
local nextIndex = 1
for i, anim in ipairs(animations) do
if anim == current then
nextIndex = i + 1
break
end
end
if nextIndex > #animations then
nextIndex = 1
end
return animations[nextIndex]
end
function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
function UntagHumanoid(humanoid)
for i, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
local function OnToolActivated(tool)
local swordStatus = Character:FindFirstChild("SwordStatus")
if not Humanoid or not swordStatus then
return
end
if swordStatus.Value == "" then
swordStatus.Value = "Attacking"
local animation = Instance.new("Animation")
animation.AnimationId = animationIds[currentAnimation]
local animationTrack = Humanoid.Animator:LoadAnimation(animation)
animationTrack:Play()
currentAnimation = GetNextAnimation(currentAnimation)
animationTrack.Stopped:Connect(function()
if swordStatus.Value == "Attacking" then
swordStatus.Value = ""
print("Animation ended, forced stop")
end
end)
local hitbox = Character:FindFirstChild("HumanoidRootPart"):FindFirstChild("Hitbox")
local overlapParams = OverlapParams.new()
overlapParams.FilterType = Enum.RaycastFilterType.Exclude
overlapParams.FilterDescendantsInstances = { Character }
overlapParams.MaxParts = 10
local partsInHitbox = Workspace:GetPartsInPart(hitbox, overlapParams)
local hitDetected = false
for _, part in ipairs(partsInHitbox) do
if part:IsA("BasePart") and part.Parent ~= Character then
local enemyCharacter = part.Parent
print("Hitbox detected: ", part:GetFullName())
if enemyCharacter and enemyCharacter:FindFirstChild("Humanoid") then
print("Enemy detected:", enemyCharacter.Name)
if enemyCharacter:FindFirstChildOfClass("ForceField") then
print("Enemy has a ForceField, skipping damage and knockback")
continue
end
local enemyHumanoidRootPart = enemyCharacter:FindFirstChild("HumanoidRootPart")
SS.SwordHit:Play()
local damage = (currentAnimation == "Stab4") and 30 or 10
enemyCharacter.Humanoid:TakeDamage(damage)
print("Dealing damage to:", enemyCharacter.Name)
UntagHumanoid(enemyCharacter.Humanoid)
TagHumanoid(enemyCharacter.Humanoid, Player)
if enemyHumanoidRootPart then
local knockbackForce = Instance.new("BodyVelocity")
knockbackForce.MaxForce = Vector3.new(100000, 0, 100000)
knockbackForce.P = 1000
knockbackForce.Velocity = hitbox.CFrame.LookVector * ((currentAnimation == "Stab4") and 50 or 30)
knockbackForce.Parent = enemyHumanoidRootPart
game:GetService("Debris"):AddItem(knockbackForce, 0.2)
end
hitDetected = true
break
end
end
end
if not hitDetected then
print("Nothing was hit")
SS.SwordSlash:Play()
end
end
end
local function onBlockEvent(player)
local character = player.Character
if not character then return end
local humanoid = character:FindFirstChildOfClass("Humanoid")
local combatStatus = character:FindFirstChild("SwordStatus")
if humanoid and combatStatus then
if combatStatus.Value == "" or combatStatus.Value == "Blocking" then
if combatStatus.Value == "" then
combatStatus.Value = "Blocking"
local blockAnimation = humanoid.Animator:LoadAnimation(script.Block)
blockAnimation:Play()
wait(0.5)
if combatStatus.Value == "Blocking" then
combatStatus.Value = ""
blockAnimation:Stop()
print("Blocking ended automatically after 0.5 seconds")
end
local updateConnection
updateConnection = combatStatus.Changed:Connect(function(newStatus)
if newStatus ~= "Blocking" then
blockAnimation:Stop()
updateConnection:Disconnect()
end
end)
blockAnimation.Stopped:Connect(function()
if combatStatus.Value == "Blocking" then
combatStatus.Value = ""
print("Blocking ended automatically")
end
end)
end
else
combatStatus.Value = ""
end
end
end
script.Parent.BlockEvent.OnServerEvent:Connect(onBlockEvent)
local function OnToolEquipped()
Character = Tool.Parent
Player = Players:GetPlayerFromCharacter(Character)
Humanoid = Character:FindFirstChildOfClass("Humanoid")
end
Tool.Activated:Connect(OnToolActivated)
Tool.Equipped:Connect(OnToolEquipped)
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
end)
end)
Thanks for helping!