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

For some reason, the NPC won't damage the player (?)

Hello!

I made this script which basically dodges, and damage the player, the problem Is that the NPC sword won't damage the player

Script:

local Noob = script.Parent
local CanDamage = false

while wait(math.random(1,2)) do
	local AttackType = math.random(1,4)
	
	if AttackType == 1 then
		Noob.SwordSlash:Play()
		CanDamage = true
		game:GetService("Chat"):Chat(Noob.Head,"Die!",Enum.ChatColor.White)
		local AttackSlash = Noob.Humanoid:FindFirstChild("Animator"):LoadAnimation(Noob.Slash)
		AttackSlash:Play()
		wait(1)
		CanDamage = false
	end
	if AttackType == 2 then
		CanDamage = true
		Noob.Humanoid.Jump = true
		wait(.05)
		game:GetService("Chat"):Chat(Noob.Head,"INCOMING!!!",Enum.ChatColor.White)
		Noob.SwordLunge:Play()
		local VectorForce = Instance.new("VectorForce",Noob.Torso)
		local Attachment = Instance.new("Attachment",Noob.Torso)
		VectorForce.Attachment0 = Attachment
		VectorForce.Force = Vector3.new(0,300,-6000)
		VectorForce.ApplyAtCenterOfMass = true
		game:GetService("Debris"):AddItem(VectorForce,0.5)
		game:GetService("Debris"):AddItem(Attachment,0.5)
		local AttackSlash = Noob.Humanoid:FindFirstChild("Animator"):LoadAnimation(Noob.Lunge)
		AttackSlash:Play()
		wait(1)
		CanDamage = false
	end
	if AttackType == 3 then
		CanDamage = true
		Noob.Humanoid.Jump = true
		wait(.05)
		game:GetService("Chat"):Chat(Noob.Head,"Skill Issue XD",Enum.ChatColor.White)
		local VectorForce = Instance.new("VectorForce",Noob.Torso)
		local Attachment = Instance.new("Attachment",Noob.Torso)
		VectorForce.Attachment0 = Attachment
		VectorForce.Force = Vector3.new(6000,300,0)
		VectorForce.ApplyAtCenterOfMass = true
		game:GetService("Debris"):AddItem(VectorForce,0.5)
		game:GetService("Debris"):AddItem(Attachment,0.5)
	end
	if AttackType == 4 then
		CanDamage = true
		Noob.Humanoid.Jump = true
		wait(.05)
		game:GetService("Chat"):Chat(Noob.Head,"You hate me already? Admit It!",Enum.ChatColor.White)
		local VectorForce = Instance.new("VectorForce",Noob.Torso)
		local Attachment = Instance.new("Attachment",Noob.Torso)
		VectorForce.Attachment0 = Attachment
		VectorForce.Force = Vector3.new(-6000,300,0)
		VectorForce.ApplyAtCenterOfMass = true
		game:GetService("Debris"):AddItem(VectorForce,0.5)
		game:GetService("Debris"):AddItem(Attachment,0.5)
	end
end

Noob.Sword.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:WaitForChild("Humanoid")
	if Humanoid and CanDamage == true then
		Noob["Sword Stab"]:Play()
		Humanoid:TakeDamage(30)
		CanDamage = false
	end
end)

1 answer

local Noob = script.Parent
local CanDamage = false

while task.wait(math.random(1,2)) do
    local AttackType = Random.new(os.time()):NextInterger(1,4)

    if AttackType == 1 then
        Noob.SwordSlash:Play()
        CanDamage = true
        game:GetService("Chat"):Chat(Noob:WaitForChild("Head"),"Die!",Enum.ChatColor.White)
        local AttackSlash = Noob:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(Noob:WaitForChild("Slash"))
        AttackSlash:Play()
        task.wait(1)
        CanDamage = false
    end
    if AttackType == 2 then
        CanDamage = true
        Noob:WaitForChild("Humanoid").Jump = true
        task.wait(.05)
        game:GetService("Chat"):Chat(Noob:WaitForChild("Head"),"INCOMING!!!",Enum.ChatColor.White)
        Noob.SwordLunge:Play()
        local VectorForce = Instance.new("VectorForce",Noob:WaitForChild("Torso"))
        local Attachment = Instance.new("Attachment",Noob:WaitForChild("Torso"))
        VectorForce.Attachment0 = Attachment
        VectorForce.Force = Vector3.new(0,300,-6000)
        VectorForce.ApplyAtCenterOfMass = true
        game:GetService("Debris"):AddItem(VectorForce,0.5)
        game:GetService("Debris"):AddItem(Attachment,0.5)
        local AttackSlash = Noob:WaitForChild("Humanoid"):WaitForChild("Animator"):LoadAnimation(Noob:WaitForChild("Slash"))
        AttackSlash:Play()
        task.wait(1)
        CanDamage = false
    end
    if AttackType == 3 then
        CanDamage = true
        Noob:WaitForChild("Humanoid").Jump = true
        task.wait(.05)
        game:GetService("Chat"):Chat(Noob:WaitForChild("Head"),"Skill Issue XD",Enum.ChatColor.White)
        local VectorForce = Instance.new("VectorForce",Noob:WaitForChild("Torso"))
        local Attachment = Instance.new("Attachment",Noob:WaitForChild("Torso"))
        VectorForce.Attachment0 = Attachment
        VectorForce.Force = Vector3.new(6000,300,0)
        VectorForce.ApplyAtCenterOfMass = true
        game:GetService("Debris"):AddItem(VectorForce,0.5)
        game:GetService("Debris"):AddItem(Attachment,0.5)
        task.wait(1)
        CanDamage = false
    end
    if AttackType == 4 then
        CanDamage = true
        Noob:WaitForChild("Humanoid").Jump = true
        task.wait(.05)
        game:GetService("Chat"):Chat(Noob:WaitForChild("Head"),"You hate me already? Admit It!",Enum.ChatColor.White)
        local VectorForce = Instance.new("VectorForce",Noob:WaitForChild("Torso"))
        local Attachment = Instance.new("Attachment",Noob:WaitForChild("Torso"))
        VectorForce.Attachment0 = Attachment
        VectorForce.Force = Vector3.new(-6000,300,0)
        VectorForce.ApplyAtCenterOfMass = true
        game:GetService("Debris"):AddItem(VectorForce,0.5)
        game:GetService("Debris"):AddItem(Attachment,0.5)
        task.wait(1)
        CanDamage = false
    end
end

Noob.Sword.Touched:Connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if Humanoid and CanDamage == true then
        Noob["Sword Stab"]:Play()
        Humanoid:TakeDamage(30)
    end
end)

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