I have made a ability and Sword script! I want to make them shorter, and I also want to know how I can easily perfect them so they can have no issues! The sword kinda works, but when it hits 2 body parts it still inflicts extra damage, which I don’t want.
Here is the Abilities Code
local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local CooldownAbility1 = false
local CooldownAbility2 = false
local CooldownTimer1 = 15
local CooldownTimer2 = 25
local ShiftIsHoldedDown = false
local player = Players.LocalPlayer
local character = player.CharacterAdded:Wait()
local Head = character:WaitForChild("Head")
local Face = Head:WaitForChild("Face")
local Torso = character:WaitForChild("Torso")
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Humanoid = character:WaitForChild("Humanoid")
local LeftArm = character:WaitForChild("Left Arm")
local RightArm = character:WaitForChild("Right Arm")
local LeftLeg = character:WaitForChild("Left Leg")
local RightLeg = character:WaitForChild("Right Leg")
UIS.InputBegan:Connect(function(input, GPE)
if input.KeyCode == Enum.KeyCode.Q then
if not CooldownAbility1 then
CooldownAbility1 = true
character.Humanoid.WalkSpeed = 20
wait(10)
for i = 1, 4 do
wait(0.5)
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - i
end
wait(CooldownTimer1)
CooldownAbility1 = false
end
end
end)
local function onButtonPress()
if not CooldownAbility1 then
CooldownAbility1 = true
character.Humanoid.WalkSpeed = 20
wait(10)
for i = 1, 4 do
wait(0.5)
character.Humanoid.WalkSpeed = character.Humanoid.WalkSpeed - i
end
wait(CooldownTimer1)
CooldownAbility1 = false
end
end
ContextActionService:BindAction("Sprint", onButtonPress, true, nil)
ContextActionService:SetPosition("Sprint", UDim2.new(0.088, 0,0.146, 0))
UIS.InputBegan:Connect(function(input, GPE)
if input.KeyCode == Enum.KeyCode.E then
if not CooldownAbility2 then
CooldownAbility2 = true
for i = 1, 5 do
Head.Transparency = 0.5
Face.Transparency = 0.5
Torso.Transparency = 0.5
LeftArm.Transparency = 0.5
RightArm.Transparency = 0.5
LeftLeg.Transparency = 0.5
RightLeg.Transparency = 0.5
wait(0.5)
Head.Transparency = 1
Face.Transparency = 1
Torso.Transparency = 1
LeftArm.Transparency = 1
RightArm.Transparency = 1
LeftLeg.Transparency = 1
RightLeg.Transparency = 1
wait(3)
Head.Transparency = 0
Face.Transparency = 0
Torso.Transparency = 0
LeftArm.Transparency = 0
RightArm.Transparency = 0
LeftLeg.Transparency = 0
RightLeg.Transparency = 0
end
wait(CooldownTimer2)
CooldownAbility2 = false
end
end
end)
Here is my sword script
-- Variables
local Debounce = false
local Length = 0.5
local Cooldown = 0.3
local canHit = false
local randomSlash = 1
local idle
local Slash
local SlashSound = script.Parent.Handle:WaitForChild("SlashSound")
local SlashAnim1 = script.Slash1Anim
local SlashAnim2 = script.Slash2Anim
-- Configure
local con = {
trailEnabled = true;
bloodEffects = true;
damage = function() return math.random(10, 25) end
}
-- Equip
script.Parent.Equipped:Connect(function()
script.Parent.Handle.EquipSound:Play()
end)
-- Sword Slash
script.Parent.Activated:Connect(function()
if not Debounce then
Debounce = true
if con.trailEnabled == true then
script.Parent.Handle.Effect.Enabled = true
end
local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
if not Slash then
if randomSlash == 1 then
Slash = humanoid:LoadAnimation(SlashAnim1)
randomSlash = randomSlash + 1
elseif randomSlash == 2 then
Slash = humanoid:LoadAnimation(SlashAnim2)
randomSlash = randomSlash - 1
end
end
Slash:Play()
wait(.2)
SlashSound:Play()
wait(.8)
if Slash then
Slash:Stop()
Slash = nil
end
Debounce = false
end
end)
-- Touch Function
script.Parent.Handle.Touched:Connect(function(hit)
if Debounce then
if not canHit then
canHit = true
local effect = script.Parent.Handle.Effect:Clone()
if hit.Parent:FindFirstChild("Humanoid") then
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
local hitSFX = Instance.new("Sound",hit.Parent.Head); hitSFX.SoundId = "rbxassetid://566593606"; hitSFX:Play()
con.damage = function() return math.random(25, 40) end
-- then, we call it like so:
Humanoid:TakeDamage(con.damage())
if con.bloodEffects == true then
effect.Parent = hit
effect.Enabled = true
end
end
wait(1)
effect:Destroy()
canHit = false
end
end
end)
-- Slash
local Character = game.Players.LocalPlayer.Character--Not sure how many parents but you should be able to figure it out
local Animations = {
Slash1 = script:WaitForChild("Slash1Anim"),
Slash2 = script:WaitForChild("Slash2Anim"),
}
local Move = 1
script.Parent.Activated:Connect(function()
if Debounce == false then
Debounce = true
canHit = true
if Move == 1 then
local Slash1 = Character.Humanoid:LoadAnimation(Animations.Slash1)
Slash1:Play()
SlashSound:Play()
Move = 2
elseif Move == 2 then
local Slash2 = Character.Humanoid:LoadAnimation(Animations.Slash2)
Slash2:Play()
SlashSound:Play()
Move = 1
end
wait(Length)
canHit = false
wait(Cooldown)
Debounce = false
end
end)