Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local plr = script.Parent.Parent.Parent
- local char = plr.CharacterAdded:wait()
- local hum = char:WaitForChild'Humanoid'
- local tool = script.Parent
- local debris = game:service'Debris'
- local anims = {
- combatIdle = {'rbxassetid://0'},
- attack = {'rbxassetid://0'},
- }
- local soundIndex = {
- punch = 'rbxassetid://0',
- equip = 'rbxassetid://0',
- }
- local hitboxes = {}
- local attribs = {baseDmg = 5, maxRange = 2}
- local lastPlayed = nil
- local function playAnimation(type,index)
- local anim = hum:LoadAnimation(anims.type[index])
- anim:Play()
- lastPlayed = anim
- end
- local function stopAnimation(name)
- for _,anim in pairs(hum:GetPlayingAnimationTracks()) do
- if anim.Name == name then
- anim:Stop()
- end
- end
- end
- local function playSound(source,volume,id)
- local sound = Instance.new('Sound',source)
- sound.Volume = volume
- sound.SoundId = id
- sound:Play()
- wait()
- debris:AddItem(sound,sound.TimeLength)
- end
- local function initWeld()
- local p1 = Instance.new('Part',char)
- p1.Size = Vector3.new(char:WaitForChild'Left Arm'.Size.X,2,char:WaitForChild'Left Arm'.Size.Z)
- p1.BrickColor = BrickColor.Red()
- p1.Transparency = .5
- local p2 = p1:clone()
- p2.Parent = char
- table.insert(hitboxes,p1)
- table.insert(hitboxes,p2)
- local w1 = Instance.new('Weld',p1)
- w1.Part0 = p1
- w1.Part1 = char['Left Arm']
- w1.C0 = CFrame.new(0,0,1.1) -- jeton test!!!
- local w2 = Instance.new('Weld',p2)
- w2.Part0 = p1
- w2.Part1 = char['Right Arm']
- w2.C0 = CFrame.new(0,0,1.1) -- jeton test!!!
- end
- tool.Equipped:connect(function())
- playAnimation(idle,1)
- initWeld()
- playSound(char:WaitForChild'HumanoidRootPart',.5,soundIndex.equip)
- end)
- tool.Unequipped:connect(function())
- stopAnimation('combatIdle')
- for _,v in pairs(hitboxes) do
- v:Destroy()
- end
- end
- local function calcRange(obj)
- local magnitudes = {}
- local inRange = false
- for _,p in pairs(obj:GetChildren()) do
- if p:IsA'Union' or p:IsA'Part' or p:IsA'MeshPart' then
- local mag = (hitboxes[hitbox].Position - p.Position).magnitude
- table.insert(magnitudes,mag)
- end
- end
- for _,mag in pairs(magnitudes) do
- if mag <= attribs.maxRange and not inRange then
- inRange = true
- end
- end
- return inRange
- end
- local function hitFrom(object)
- for _,victim in pairs(workspace:GetChildren()) do
- if victim:FindFirstChild'Humanoid' and victim:FindFirstChild'HumanoidRootPart' then
- if calcRange(victim) then
- victim.Humanoid:TakeDamage(attribs.baseDmg)
- end
- end
- end
- end
- local swinging = false
- local currentAnim = 1
- local maxAnims = #anims.attack
- local delay = .1
- local hitbox = 1
- tool.Activated:connect(function())
- if char:FindFirstChild'stun' then return end
- if not swinging then
- swinging = true
- if currentAnim > maxAnims then
- currentAnim = 1
- end
- playAnimation('attack',currentAnim)
- wait(lastPlayed.AnimationLength + delay)
- hitFrom(hitboxes[hitbox])
- if hitbox == 1 then
- hitbox = 2
- else
- hitbox = 1
- end
- wait(cd)
- swinging = false
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement