I need help with this NPC Control script

Ahoy! I would like to make a script where when I click the torso of the NPC, the npc then follows the player’s mouse. And when the player mousebutton2 clicks then the npc attacks the point where the mouse is!

With my code so far, it comes up with this error:

12:29:10.966 - Workspace.Pirate.Torso.Script:15: attempt to index nil with ‘Hit’

Here is my code:

script.Parent.ClickDetector.MouseHoverEnter:connect(function()
	script.Parent.SelectionBox.Visible = true
end)

script.Parent.ClickDetector.MouseHoverLeave:Connect(function()
	script.Parent.SelectionBox.Visible = false
end)

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	script.Parent.SelectionBox.Visible = true
	print(plr.Name)
	while true do
		local pos
		local m = plr:GetMouse()
		local t = m.Hit
		if t then
			pos = t
			local tor = script.Parent
			local h = script.Parent.Parent:WaitForChild("Head")
			tor.CFrame = CFrame.new(tor.Position,Vector3.new(t.X,tor.Position.Y,t.Z))
		end
		wait()
	end
end)

4 Likes

if thats a script u cant get the mouse from script u can only get it from local script

2 Likes

Also, why are you using a click detector for this? You should just have the client give the server mouse info that it needs to calculate the position.

2 Likes

@lifacy is right u should use local script to fire a Server event with the mouse hit position in it

1 Like

You also can’t :GetMouse from the server, which I believe this is using a script and not a LocalScript. This would be why m = nil.

If I use a local script it doesn’t seem to work how would I get the player who clicked mouse?

local plr = game:GetService("Players").LocalPlayer

like this?

local plr = game:GetService("Players").LocalPlayer


script.Parent.ClickDetector.MouseHoverEnter:connect(function(plr)
	script.Parent.SelectionBox.Visible = true
end)

script.Parent.ClickDetector.MouseHoverLeave:Connect(function(plr)
	script.Parent.SelectionBox.Visible = false
end)

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	script.Parent.SelectionBox.Visible = true
	print(plr.Name)
	while true do
		local pos
		local m = plr:GetMouse()
		local t = m.Hit
		if t then
			pos = t
			local tor = script.Parent
			local h = script.Parent.Parent:WaitForChild("Head")
			tor.CFrame = CFrame.new(tor.Position,Vector3.new(t.X,tor.Position.Y,t.Z))
		end
		wait()
	end
end)

Yes if you want to stick with using a LocalScript for the project. Otherwise you have to get the plr mouse.Hit and send it over to the server with a RemoteEvent as @lifacy mentioned.

When I do it it doesn’t show any errors but also nothing happens…

What I am assuming is that you have a ClickDetector on the NPC torso. When you click the torso does the ClickDetector fire? It should be printing plr.Name if it does.

It does not print (plr.Name) so no I guess the click detector does not fire :confused:

I thought that would be the issue. HumanoidRootPart is most likely blocking the torso. Attach the ClickDetector to the HumanoidRootPart or make an invisible part that is bigger than the torso and attach it to the NPC then add the ClickDetector there.

1 Like

It still doesn’t seem to be firing, here is my setup hope this helps…

This is because the LocalScript can only run from certain locations. Place the LocalScript in StarterPlayerScripts and change the variables to point to the correct locations.

IE script.Parent.ClickDetector would become workspace.Pirate.Click.ClickDetector

Or wherever it happens to be located.

Ok there is a new problem now, its printing p.Name but its coming up with this error

14:49:59.927 - Unable to cast CoordinateFrame to Vector3

local plr = game:GetService("Players").LocalPlayer


workspace.Pirate.Click.ClickDetector.MouseHoverEnter:connect(function(plr)
	workspace.Pirate.Click.SelectionBox.Visible = true
end)

workspace.Pirate.Click.ClickDetector.MouseHoverLeave:Connect(function(plr)
	workspace.Pirate.Click.SelectionBox.Visible = false
end)

workspace.Pirate.Click.ClickDetector.MouseClick:Connect(function(plr)
	workspace.Pirate.Click.SelectionBox.Visible = true
	print(plr.Name)
	while true do
		local pos
		local m = plr:GetMouse()
		local t = m.Hit
		if t then
			pos = t
			workspace.Pirate.Humanoid:MoveTo(m.Hit)
			wait()
			local tor =workspace.Pirate.Torso
			local h = workspace.Pirate:WaitForChild("Head")
			tor.CFrame = CFrame.new(tor.Position,Vector3.new(t.X,tor.Position.Y,t.Z))
			
		end
		wait()
	end
end)

heres my script btw.

That is because :MoveTo requires a Vector3 as the position, mouse.Hit returns a CFrame.

workspace.Pirate.Humanoid:MoveTo(m.Hit.Position)

That should clear that up.