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!
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)
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.
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.
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.
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
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)