I'm trying to make a gui where you click a button and it sends an NPC to the corresponding button you clicked (For example ICU would make the NPC go to the ICU)
--In localScript inside the player's GUI.
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local npc = workspace.NPC
local followingPath = false
script.Parent.ICU.MouseButton1Click:connect(function()
if not followingPath then
followingPath = true
local points = game.ReplicatedStorage.RemoteFunction:InvokeServer(workspace.Dests.ICU.Value)
for _, point in ipairs(points) do
print("moving to: ", point)
npc.Humanoid:MoveTo(point)
repeat
distance = (point - npc.Torso.Position).magnitude
wait()
until distance < 3
end
followingPath = false
end
end)
script.Parent.Entrance.MouseButton1Click:connect(function()
if not followingPath then
followingPath = true
local points = game.ReplicatedStorage.RemoteFunction:InvokeServer(workspace.Dests.Entrance.Value)
for _, point in ipairs(points) do
print("moving to: ", point)
npc.Humanoid:MoveTo(point)
repeat
distance = (point - npc.Torso.Position).magnitude
wait()
until distance < 3
end
followingPath = false
end
end)
-- In Script in ServerScriptService
local pathfindingService = game:GetService("PathfindingService")
local pointModel = Instance.new("Model")
pointModel.Name = "Points"
pointModel.Parent = game.Workspace
-- Setup remote function
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Parent = game.ReplicatedStorage
function visualizePath(path)
-- clear old path visualization
pointModel:ClearAllChildren()
-- calculate new visualization
local points = path:GetPointCoordinates()
for _, point in ipairs(points) do
local part = Instance.new("Part")
part.Parent = pointModel
part.FormFactor = Enum.FormFactor.Custom
part.Size = Vector3.new(1,1,1)
part.Position = point
part.Anchored = true
part.CanCollide = false
end
end
game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(player, target)
local start = player.Torso.Position
local path = pathfindingService:ComputeRawPathAsync(start, target, 500)
visualizePath(path)
-- Check to see if the path was computed correctly
if path.Status == Enum.PathStatus.FailStartNotEmpty or path.Status == Enum.PathStatus.FailFinishNotEmpty then
print("Compute failed")
return {}
end
return path:GetPointCoordinates()
end
But my error is,
22:26:09.251 - Torso is not a valid member of Player
22:26:09.252 - Script 'Players.Player1.PlayerGui.ScreenGui.Frame.Script', Line 24
22:26:09.254 - Stack End
