I want to make an NPC go towards the allocated player but I need the goal for the NPC to be, Goal = game.Players.(nearestplayer) but you cannot put brackets in the statement so I cant fix the issue, I’ve tried using tables because I cant do, Goal = game.Players., nearestplayer But they still don’t work
local Players = game:GetService("Players")
local part = game.Workspace.Harvey.Head
local maxDistance = 10000
while true do
wait(1)
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
character = player.Character
distance = player:DistanceFromCharacter(part.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
print("The nearest player is ", nearestPlayer)
end
-------------------------------------------------------------------
--Import the module so you can start using it
local ServerStorage = game:GetService("ServerStorage")
local SimplePath = require(ServerStorage.SimplePath)
--Define npc
local Dummy = game.workspace.Harvey
-- Define a part called "Goal"
Goal = game.Players.(nearestplayer)
--Create a new Path using the Dummy
local Path = SimplePath.new(Dummy)
--Helps to visualize the path
Path.Visualize = true
while true do
Path:Run(Goal)
end
(I’m also using ‘SimplePath’ for this NPC if you need to know)
Btw you are skipping an iteration if a player’s character does exist, so that is strange a little bit. Are you trying to chase ghosts?
And why are you using findfirstchild if nearestplayer is what you are looking for?
Only that you need to get is a .Character reference…
Goal is a table and table needs string or instance not a script.
You can try by just simply put the player name in there nearestplayer.Name to put a string or game.Players[nearestplayer.Name] to put a instance
Sorry if im wrong.
while true do
wait(1)
local nearestPlayer, nearestDistance = nil,maxDistance
for _, player in pairs(Players:GetPlayers()) do
character = player.Character
distance = player:DistanceFromCharacter(part.Position)
if not character or distance >= nearestDistance then
continue
end
nearestDistance = distance
nearestPlayer = player
end
print("The nearest player is ", nearestPlayer)
goal = nearestPlayer
end