Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix the default sword which roblox made, dealing damage even animation doesn't run?

Sword touches somebody and it deals damage while equipped, but I don't swing it. This is the Sword script just in case:

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = 5,
	SlashDamage = 5,
	LungeDamage = 5
}

--For R15 avatars
Animations = {
	R15Slash = 3014161653,
	R15Lunge = 3014161653
}

Damage = DamageValues.BaseDamage

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function tagHumanoid(humanoid, player)
	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = player
	creator_tag.Name = "creator"
	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

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	UntagHumanoid(humanoid)
	tagHumanoid(humanoid, Player)
	humanoid:TakeDamage(Damage)	
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()
	

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage
	Sounds.Lunge:Play()
	
	
	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	

	--[[if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end]]
	

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		Lunge()
	else
		Attack()
	end
	LastAttack = Tick
	wait(1.4)-- How long until you can strike again
	Damage = DamageValues.BaseDamage
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
		Name = "R15Lunge",
		AnimationId = BaseUrl .. Animations.R15Lunge,
		Parent = Tool
	})
	Tool.Enabled = true
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	ToolEquipped = false
end


Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

`

1 answer

✓ Accepted answer

Replace line 100 with these four lines:

local Tick = RunService.Stepped:wait()
if (Tick - LastAttack < 0.2) then
	humanoid:TakeDamage(Damage)
end

This will make it so it will only damage the player if the sword's attack has been activated recently.

Full Script:

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

function Create(ty)
    return function(data)
        local obj = Instance.new(ty)
        for k, v in pairs(data) do			if type(k) == 'number' then
            v.Parent = obj
        else
            obj[k] = v
        end
        end
        return obj
    end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
    BaseDamage = 5,
    SlashDamage = 5,
    LungeDamage = 5
}

--For R15 avatars
Animations = {
    R15Slash = 3014161653,
    R15Lunge = 3014161653
}

Damage = DamageValues.BaseDamage

Sounds = {
    Slash = Handle:WaitForChild("SwordSlash"),
    Lunge = Handle:WaitForChild("SwordLunge"),
    Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
    if v:IsA("ParticleEmitter") then
        v.Rate = 20
    end
end

Tool.Enabled = true

function IsTeamMate(Player1, Player2)
    return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function tagHumanoid(humanoid, player)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    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

function Blow(Hit)
    if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
        return
    end
    local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
    if not RightArm then
        return
    end
    local RightGrip = RightArm:FindFirstChild("RightGrip")
    if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
        return
    end
    local character = Hit.Parent
    if character == Character then
        return
    end
    local humanoid = character:FindFirstChildOfClass("Humanoid")
    if not humanoid or humanoid.Health == 0 then
        return
    end
    local player = Players:GetPlayerFromCharacter(character)
    if player and (player == Player or IsTeamMate(Player, player)) then
        return
    end
    UntagHumanoid(humanoid)
    tagHumanoid(humanoid, Player)
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		humanoid:TakeDamage(Damage)
	end
end

function Attack()
    Damage = DamageValues.SlashDamage
    Sounds.Slash:Play()


    if Humanoid then
        if Humanoid.RigType == Enum.HumanoidRigType.R6 then
            local Anim = Instance.new("StringValue")
            Anim.Name = "toolanim"
            Anim.Value = "Slash"
            Anim.Parent = Tool
        elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
            local Anim = Tool:FindFirstChild("R15Slash")
            if Anim then
                local Track = Humanoid:LoadAnimation(Anim)
                Track:Play(0)
            end
        end
    end
end

function Lunge()
    Damage = DamageValues.LungeDamage
    Sounds.Lunge:Play()


    if Humanoid then
        if Humanoid.RigType == Enum.HumanoidRigType.R6 then
            local Anim = Instance.new("StringValue")
            Anim.Name = "toolanim"
            Anim.Value = "Lunge"
            Anim.Parent = Tool
        elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
            local Anim = Tool:FindFirstChild("R15Lunge")
            if Anim then
                local Track = Humanoid:LoadAnimation(Anim)
                Track:Play(0)
            end
        end
    end

    --[[if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end]]


    Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
    if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
        return
    end
    Tool.Enabled = false
    local Tick = RunService.Stepped:wait()
    if (Tick - LastAttack < 0.2) then
        Lunge()
    else
        Attack()
    end
    LastAttack = Tick
    wait(1.4)-- How long until you can strike again
    Damage = DamageValues.BaseDamage
    local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
        Name = "R15Slash",
        AnimationId = BaseUrl .. Animations.R15Slash,
        Parent = Tool
    })

    local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
        Name = "R15Lunge",
        AnimationId = BaseUrl .. Animations.R15Lunge,
        Parent = Tool
    })
    Tool.Enabled = true
end

function CheckIfAlive()
    return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
    Character = Tool.Parent
    Player = Players:GetPlayerFromCharacter(Character)
    Humanoid = Character:FindFirstChildOfClass("Humanoid")
    Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
    if not CheckIfAlive() then
        return
    end
    ToolEquipped = true
    Sounds.Unsheath:Play()
end

function Unequipped()
    ToolEquipped = false
end


Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

2 comments

0

I put it like this and it still doesn't work UntagHumanoid(humanoid) local Tick = RunService.Stepped:wait() if (Tick - LastAttack < 0.2) then humanoid:TakeDamage(Damage) end humanoid:TakeDamage(Damage) end

0

I don't know that you pasted it right, as it's working fine for me. I'll just post the whole script I guess.

This discussion is preserved as it was. New answers, comments, and votes are closed.