Roblox code help with NPCS Attacking Players

Hi, I need help with making a GUI to allow the player to make the selected NPC in workspace.NPCS to attack the selected player

Ok, we need more information. Please provide:

  1. Your current code
  2. The Explorer
1 Like

I have a lot of code, are you sure you want me to do that?

Don’t you want me to… Help you?

1 Like

ok, give me a second and ill send you all of it

Screenshot 2023-05-28 190705

Screenshot 2023-05-28 190749

Here are my scripts:
attackscript:

script.Parent.AttackEvent.OnServerEvent:Connect(function(localplr2, targetedplayer, attackingnpc, localplr) --This detects if the ControlEvent is fired.
	local foundplr = game.Workspace[targetedplayer]
	local foundnpc = game.Workspace.NPCS[attackingnpc]
	local move = foundnpc.move
	local clonesword = script.ClassicSword:Clone()
	local lol2 = true
	clonesword.Parent = foundnpc
	foundnpc.Humanoid:EquipTool(clonesword)
	local be = script.lol
	be.Parent = foundnpc
	while lol2 == true do
		wait(1)
		foundnpc.Humanoid:MoveTo(foundplr.HumanoidRootPart.Position, foundplr.HumanoidRootPart)
		be:Fire()
	end
	foundplr.Humanoid.Died:Connect(function()
		foundnpc.Humanoid:UnequipTools()
		be.Parent = script
		clonesword:Destroy()
		move.Parent = foundnpc
		lol2 = false
	end)
end)

SwordScript:

--Rescripted by Luckymaxer
--EUROCOW WAS HERE BECAUSE I MADE THE PARTICLES AND THEREFORE THIS ENTIRE SWORD PRETTY AND LOOK PRETTY WORDS AND I'D LIKE TO DEDICATE THIS TO MY FRIENDS AND HI LUCKYMAXER PLS FIX SFOTH SWORDS TY LOVE Y'ALl
--Updated for R15 avatars by StarWars
--Re-updated by TakeoHonorable

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")
local be = Tool.Parent.lol

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://"


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

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

--For R15 avatars
Animations = {
	R15Slash = 522635514,
	R15Lunge = 522638767
}

Damage = DamageValues.BaseDamage

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

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.Grip = Grips.Up
Tool.Enabled = true


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

	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
	]]
	
	wait(0.2)
	Tool.Grip = Grips.Out
	wait(0.6)
	Tool.Grip = Grips.Up

	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(0.5)
	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 (((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
	
	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()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

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

Connection = Handle.Touched:Connect(Blow)
be.Event:Connect(Activated)

Local script in button named Attack:

--Control Button Script
--Unless you know what you're doing, do not edit this script.
script.Parent.MouseButton1Click:Connect(function() --This detects if the control button is clicked.
	script.Parent.Parent.AttackEvent:FireServer(script.Parent.Parent.SelectedPlayer.Value, script.Parent.Parent.SelectedNPC.Value, game.Players.LocalPlayer)
end)
TextBoxScript:
--Text Box Script
--This script basically changes values for the targeted player.
--Unless you know what you're doing, do not edit this script.
script.Parent.NameBox.Changed:Connect(function() --This detects if the name box is changed.
	script.Parent.SelectedPlayer.Value = script.Parent.NameBox.Text --This sets the SelectedPlayer value to the text of the name box.
end)
script.Parent.NPCBox.Changed:Connect(function()
	script.Parent.SelectedNPC.Value = script.Parent.NPCBox.Text
end)

note: MouseIcon LocalScript is not needed

also, i almost fixed it, it is just that the npc won’t stop trying to move to me and unequip the sword after i die

i have been waiting for a while for a new response please message me if you have another answer

Can you post a link to your Repl? This will allow others in the community to offer some advice.

That’s not exactly how it works

image

Thanks. I’m not clear on why they are posting the question to Ask then.