Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {}
- -- Cache frequently accessed objects
- local Debris = game:GetService("Debris")
- local slashEffects = game.ReplicatedStorage.SlashEffects:GetChildren()
- local numSlashEffects = #slashEffects
- function module.DamageHandler(tool:Tool|BasePart, hit:BasePart, knockback:number?,DoSlashes:boolean?,CritBoost:number?)
- local hitParent = hit.Parent
- local toolParent = tool.Parent
- if not hitParent or not toolParent then return end
- local hitHumanoid = hitParent:FindFirstChild("Humanoid")
- local toolHumanoid = toolParent:FindFirstChild("Humanoid")
- local hitTeam = hitParent:FindFirstChild("Team")
- local toolTeam = toolParent:FindFirstChild("Team")
- if not (hitHumanoid and toolHumanoid and hitTeam and toolTeam) then return end
- -- 0 is npcs 1 is players 3 is pvp players
- if (toolTeam.Value == 3 and hitTeam.Value == 1) or (toolTeam.Value == 1 and hitTeam.Value == 3) then
- return
- end
- if hitParent == toolParent or (toolTeam.Value ~= 3 and toolTeam.Value == hitTeam.Value) then
- return
- end
- local dmg = tool.Damage
- if not dmg then return end
- if CritBoost and CritBoost > 1 and tool:FindFirstChild("CritSlash") then
- tool.CritSlash:Play()
- elseif tool:FindFirstChild("Slash") then
- tool.Slash:Play()
- CritBoost = 1
- else
- CritBoost = 1
- end
- local char = toolParent
- local hitRootPart = hitParent:FindFirstChild("HumanoidRootPart")
- if not hitRootPart then return end
- local player = game.Players:GetPlayerFromCharacter(toolParent)
- if player then
- local tag = Instance.new("ObjectValue")
- tag.Value = player
- tag.Name = "creator"
- tag.Parent = hitHumanoid
- game:GetService("Debris"):AddItem(tag, 0.5)
- end
- local shield = hitParent:FindFirstChild("Shield")
- if shield and shield:FindFirstChild("Health") then
- local angle = math.acos(hitRootPart.CFrame.LookVector:Dot((toolParent.HumanoidRootPart.Position - hitRootPart.Position).Unit))
- if angle < math.pi/2 then
- shield.Health.Value = math.max(0, shield.Health.Value - (dmg.Value * CritBoost))
- return
- end
- end
- if DoSlashes and hitRootPart:FindFirstChild("SlashesGUI") then
- local randomSlash = slashEffects[math.random(1, numSlashEffects)]:Clone()
- if CritBoost > 1 then
- randomSlash.ImageColor3 = Color3.fromRGB(255, 0, 0)
- randomSlash.ImageTransparency = 0
- end
- randomSlash.Parent = hitRootPart.SlashesGUI
- end
- local healthValue = hitHumanoid:FindFirstChild("Health")
- if healthValue and healthValue:IsA("NumberValue") then
- hitHumanoid.Health.Value -= dmg.Value * CritBoost
- else
- hitHumanoid.Health -= dmg.Value * CritBoost
- end
- if knockback then
- hitRootPart.AssemblyLinearVelocity = (hitRootPart.Position - toolParent.HumanoidRootPart.Position).Unit * knockback
- end
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement