How do i fix this script?

i want to make it work with the humanoid i have named “Human” so what would i need to change? i’m very new to this so if you could explain what i would need to do that would be appreciated.

local larm = script.Parent:FindFirstChild(“HitBox”)
local rarm = script.Parent:FindFirstChild(“HitBox”)
local canAttack = false
local attackReady = true
local damaged = false
local attackCooldown = script.Parent.Configurations.AttackCooldown

function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local MaxDist = script.Parent.Configurations.Dist.Value
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == “Model”) and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
temp = temp2:findFirstChild(“HumanoidRootPart”)
human = temp2:findFirstChild(“Humanoid”)
if temp:IsA(“Part”) or temp:IsA(“UnionOperation”) or temp:IsA(“MeshPart”) then
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < MaxDist then
torso = temp
MaxDist = (temp.Position - pos).magnitude
end
end
end
end
end
return torso
end

function Attack(pos)
local list = game.Workspace:children()
local torso = nil
local dist = script.Parent.Configurations.AttackDist.Value
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == “Model”) and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
temp = temp2:findFirstChild(“HumanoidRootPart”)
human = temp2:findFirstChild(“Humanoid”)
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end

function fireball(pos)
local list = game.Workspace:children()
local torso = nil
local dist = script.Parent.Configurations.ShootingDist.Value
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == “Model”) and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
temp = temp2:findFirstChild(“HumanoidRootPart”)
human = temp2:findFirstChild(“Humanoid”)
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end

–HIT–
function Hit(hit)
local human = hit.Parent:FindFirstChild(“Humanoid”)
if human ~= nil and canAttack and damaged == false and game.Players:FindFirstChild(human.Parent.Name) then
damaged = true
human.Health = human.Health - script.Parent.Configurations.Damage.Value
wait(attackCooldown.Value)
damaged = false
end
end

larm.Touched:connect(Hit)

–DETECTOR
while true do
wait()
if script.Parent.Humanoid.Health > 0 then
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
local attackPos = Attack(script.Parent.HumanoidRootPart.Position)
local fireball = fireball(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
if attackPos ~= nil and attackReady == true then
attackReady = false

local speed = script.Parent.Humanoid.WalkSpeed
script.Parent.Humanoid.WalkSpeed = 0
local random = math.random(1,10)
if random < 10 then
local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
anim:Play()
end
canAttack = true
wait(0.5)
canAttack = false

wait(attackCooldown.Value-0.6)
attackReady = true
script.Parent.Humanoid.WalkSpeed = speed
end
if fireball ~= nil and attackReady == true then
attackReady = false

local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
anim:Play()
local p = game.ReplicatedStorage.ShootingBalls[script.Parent.Configurations.FireballName.Value]:Clone()
p.Parent = game.Workspace
p.CFrame = script.Parent.fireballPart.CFrame
local bv = Instance.new(“BodyVelocity”)
bv.Name = “BV”
bv.Parent = p
bv.velocity = (fireball.Position - script.Parent.Head.Position).unit*60
local scrupt = script.fireball:Clone()
scrupt.Parent = p
scrupt.Disabled = false
scrupt.range.Value = script.Parent.Configurations.FireballRange.Value
scrupt.damage.Value = script.Parent.Configurations.FireballDamage.Value

game:GetService(“Debris”):AddItem(p, 7)

wait(script.Parent.Configurations.FireballCooldown.Value)
attackReady = true
end
end
end

Please edit post by using the triple-backtick to quote your code so we can see the proper indentation, etc.

1 Like
local larm = script.Parent:FindFirstChild("HitBox")
local rarm = script.Parent:FindFirstChild("HitBox")
local canAttack = false
local attackReady = true
local damaged = false
local attackCooldown = script.Parent.Configurations.AttackCooldown

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local MaxDist = script.Parent.Configurations.Dist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if temp:IsA("Part") or temp:IsA("UnionOperation") or temp:IsA("MeshPart") then
				if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
					if (temp.Position - pos).magnitude < MaxDist then
						torso = temp
						MaxDist = (temp.Position - pos).magnitude
					end
				end
			end
		end
	end
	return torso
end

function Attack(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = script.Parent.Configurations.AttackDist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function fireball(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = script.Parent.Configurations.ShootingDist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

--HIT--
function Hit(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil and canAttack and damaged == false and game.Players:FindFirstChild(human.Parent.Name) then
		damaged = true
		human.Health =  human.Health - script.Parent.Configurations.Damage.Value
		wait(attackCooldown.Value)
		damaged = false
	end
end

larm.Touched:connect(Hit)

--DETECTOR
while true do
	wait()
	if script.Parent.Humanoid.Health > 0 then
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		local attackPos = Attack(script.Parent.HumanoidRootPart.Position)
		local fireball = fireball(script.Parent.HumanoidRootPart.Position)
		if target ~= nil then
			script.Parent.Humanoid:MoveTo(target.Position, target)
		end
		if attackPos ~= nil and attackReady == true then
			attackReady = false
			--
			local speed = script.Parent.Humanoid.WalkSpeed
			script.Parent.Humanoid.WalkSpeed = 0
			local random = math.random(1,10)
			if random < 10 then
				local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
				anim:Play()
			end
			canAttack = true
			wait(0.5)
			canAttack = false
			--
			wait(attackCooldown.Value-0.6)
			attackReady = true
			script.Parent.Humanoid.WalkSpeed = speed
		end
		if fireball ~= nil and attackReady == true then
			attackReady = false
			--
			local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
			anim:Play()
			local p = game.ReplicatedStorage.ShootingBalls[script.Parent.Configurations.FireballName.Value]:Clone()
			p.Parent = game.Workspace
			p.CFrame = script.Parent.fireballPart.CFrame
			local bv = Instance.new("BodyVelocity")
			bv.Name = "BV"
			bv.Parent = p
			bv.velocity = (fireball.Position - script.Parent.Head.Position).unit*60
			local scrupt = script.fireball:Clone()
			scrupt.Parent = p
			scrupt.Disabled = false
			scrupt.range.Value = script.Parent.Configurations.FireballRange.Value
			scrupt.damage.Value = script.Parent.Configurations.FireballDamage.Value
			--
			game:GetService("Debris"):AddItem(p, 7)
			--
			wait(script.Parent.Configurations.FireballCooldown.Value)
			attackReady = true
		end
	end
end ```

Looks like this script is designed to be parented to the player’s Character, so you need to get the
script object and the desired Player object, and then parent the script to the player’s Character–
Something like scrobj.Parent = player.Character

it works fine on a Humanoid AI but i want to change the Humanoid’s name of that Mob/npc To “Human” and have the script still work does this make sense?

I guess the question is–What did you try that did not work?

local larm = script.Parent:FindFirstChild("SwordHitBox")
local rarm = script.Parent:FindFirstChild("SwordHitBox")
local canAttack = false
local attackReady = true
local damaged = false
local attackCooldown = script.Parent.Configurations.AttackCooldown

function findNearestTorso(pos)
	local list = game.Workspace.TerrainMap:GetChildren()
	local torso = nil
	local dist = script.Parent.Configurations.Dist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	local mob = nil
	for i=1,#list do
		if list[i]:IsA("Folder") then
			local list2 = list[i].SpawnedMobs:GetChildren()
			for i=1,#list2 do
				local list3 = list2[i]:GetChildren()
				for i=1,#list3 do
					mob = list3[i]
					if mob.className == "Model" and mob:FindFirstChild("AI") then
						temp = mob:findFirstChild("SwordHitBox")
						--temp3 = mob:findFirstChild("SwordHitbox")
						human = mob:findFirstChild("Human")
						if temp and human and human.Health > 0 then
							if (temp.Position - pos).magnitude < dist then
								torso = temp
								dist = (temp.Position - pos).magnitude
							end
							--if temp3 and (temp3.Position - pos).magnitude < dist[1] then
							--	torso = temp3
							--	dist[1] = (temp3.Position - pos).magnitude
							--end
						end
					end
				end
			end
		end
	end
	return torso
end

function Attack(pos)
	local list = game.Workspace.TerrainMap:GetChildren()
	local torso = nil
	local dist = script.Parent.Configurations.AttackDist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	local mob = nil
	for i=1,#list do
		if list[i]:IsA("Folder") then
			local list2 = list[i].SpawnedMobs:GetChildren()
			for i=1,#list2 do
				local list3 = list2[i]:GetChildren()
				for i=1,#list3 do
					mob = list3[i]
					if mob.className == "Model" and mob:FindFirstChild("AI") then
						temp = mob:findFirstChild("SwordHitBox")
						--temp3 = mob:findFirstChild("SwordHitbox")
						human = mob:findFirstChild("Human")
						if temp and human and human.Health > 0 then
							if (temp.Position - pos).magnitude < dist then
								torso = temp
								dist = (temp.Position - pos).magnitude
							end
							--if temp3 and (temp3.Position - pos).magnitude < dist[1] then
							--	torso = temp3
							--	dist[1] = (temp3.Position - pos).magnitude
							--end
						end
					end
				end
			end
		end
	end
	return torso
end

--HIT--
--function Hit(hit)
	--[==[
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human ~= nil and damaged == false and game.Players:FindFirstChild(human.Parent.Name) then
		local random = math.random(-3,3)
		damaged = true
		human.Health =  human.Health - script.Parent.Configurations.Damage.Value
	
		--ATTACK_SOUND--
		script.Parent.Head.AttackSound.PlaybackSpeed = 3 + random*0.1
		wait()
		script.Parent.Head.AttackSound:Play()
		
		wait(attackCooldown.Value)
		damaged = false
	end
	--]==]
--end
--larm.Touched:connect(Hit)
--rarm.Touched:connect(Hit)

--DETECTOR
while true do
	wait(0.5)
	if script.Parent.Human.Health > 0 and script.Parent:FindFirstChild("HumanoidRootPart") then
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		local attackPos = Attack(script.Parent.HumanoidRootPart.Position)
		if target ~= nil then
			script.Parent.Human.WalkToPoint = Vector3.new(target.Position.X,target.Position.Y+1,target.Position.Z)
		end
		if attackPos ~= nil and attackReady == true then
			
			
			local value = Instance.new("NumberValue")
			local findValue = attackPos.Parent.Players:FindFirstChild(script.Parent.PlayerName.Value)
			if findValue then
				findValue.Value = findValue.Value + script.Parent.Configurations.Damage.Value
			else
				value.Parent = attackPos.Parent.Players
				value.Name = script.Parent.PlayerName.Value
				value.Value = value.Value + script.Parent.Configurations.Damage.Value
			end
			attackPos.Parent.Human.Health = attackPos.Parent.Human.Health - script.Parent.Configurations.Damage.Value
			
			local random = math.random(-3,3)
			damaged = true
			attackPos.Parent.Human.Health = attackPos.Parent.Human.Health - script.Parent.Configurations.Damage.Value
		
			--ATTACK_SOUND--
			script.Parent.Head.AttackSound.PlaybackSpeed = 3 + random*0.1
			wait()
			script.Parent.Head.AttackSound:Play()
			damaged = false
			
			--
			
			--local speed = script.Parent.Humanoid.WalkSpeed
			--script.Parent.Humanoid.WalkSpeed = 0
			attackReady = false
			local random = math.random(1,2)
			if random == 1 then
				local anim = script.Parent.Human:LoadAnimation(script.Parent.Attack)
				anim:Play()
			elseif random == 2 then
				local anim = script.Parent.Human:LoadAnimation(script.Parent.Attack2)
				anim:Play()
			end
			wait(0.1)
			canAttack = true
			wait(0.1)
			canAttack = false
			wait(attackCooldown.Value-0.2)
			attackReady = true
			--script.Parent.Humanoid.WalkSpeed = speed
		end
	end
end ```

If you want to do that, use this:

humanoid = character:FindFirstChildOfClass("Humanoid")

This will return a humanoid if it finds one, no matter its name.

okay ill try that thank you…

Once you have your Humanoid as @Egzekiel says, you may want to go through and use the same
technique so you don’t have to constantly use “script.Parent.Human...” This will also make it less
of a pain if you want to change it in your code to “WackyHuman” for instance.

local larm = script.Parent:FindFirstChild("HitBox")
local rarm = script.Parent:FindFirstChild("HitBox")
local canAttack = false
local attackReady = true
local damaged = false
local attackCooldown = script.Parent.Configurations.AttackCooldown

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local MaxDist = script.Parent.Configurations.Dist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:FindFirstChildOfClass("Humanoid")
			if temp:IsA("Part") or temp:IsA("UnionOperation") or temp:IsA("MeshPart") then
				if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
					if (temp.Position - pos).magnitude < MaxDist then
						torso = temp
						MaxDist = (temp.Position - pos).magnitude
					end
				end
			end
		end
	end
	return torso
end

function Attack(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = script.Parent.Configurations.AttackDist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:FindFirstChildOfClass("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

function fireball(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = script.Parent.Configurations.ShootingDist.Value
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) and (game.Players:findFirstChild(temp2.Name)) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:FindFirstChildOfClass("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

--HIT--
function Hit(hit)
	local human = hit.Parent:FindFirstChildOfClass("Humanoid")
	if human ~= nil and canAttack and damaged == false and game.Players:FindFirstChild(human.Parent.Name) then
		damaged = true
		human.Health =  human.Health - script.Parent.Configurations.Damage.Value
		wait(attackCooldown.Value)
		damaged = false
	end
end

larm.Touched:connect(Hit)

--DETECTOR
while true do
	wait()
	if script.Parent.Humanoid.Health > 0 then
		local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
		local attackPos = Attack(script.Parent.HumanoidRootPart.Position)
		local fireball = fireball(script.Parent.HumanoidRootPart.Position)
		if target ~= nil then
			script.Parent.Humanoid:MoveTo(target.Position, target)
		end
		if attackPos ~= nil and attackReady == true then
			attackReady = false
			--
			local speed = script.Parent.Humanoid.WalkSpeed
			script.Parent.Humanoid.WalkSpeed = 0
			local random = math.random(1,10)
			if random < 10 then
				local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
				anim:Play()
			end
			canAttack = true
			wait(0.5)
			canAttack = false
			--
			wait(attackCooldown.Value-0.6)
			attackReady = true
			script.Parent.Humanoid.WalkSpeed = speed
		end
		if fireball ~= nil and attackReady == true then
			attackReady = false
			--
			local anim = script.Parent.Humanoid:LoadAnimation(script.Parent.Attack)
			anim:Play()
			local p = game.ReplicatedStorage.ShootingBalls[script.Parent.Configurations.FireballName.Value]:Clone()
			p.Parent = game.Workspace
			p.CFrame = script.Parent.fireballPart.CFrame
			local bv = Instance.new("BodyVelocity")
			bv.Name = "BV"
			bv.Parent = p
			bv.velocity = (fireball.Position - script.Parent.Head.Position).unit*60
			local scrupt = script.fireball:Clone()
			scrupt.Parent = p
			scrupt.Disabled = false
			scrupt.range.Value = script.Parent.Configurations.FireballRange.Value
			scrupt.damage.Value = script.Parent.Configurations.FireballDamage.Value
			--
			game:GetService("Debris"):AddItem(p, 7)
			--
			wait(script.Parent.Configurations.FireballCooldown.Value)
			attackReady = true
		end
	end
end ``` does not work...