So basically I’m trying to make an automated AI/serving bot for my restaurant.
Right now, it does what its intended to do, I click a button, it spawns the NPC, and the NPC walks to the player.
What I want
When a player clicks a button, the NPC spawns NEAR the player (around the player), but say if the player was sitting by a window, I don’t want the NPC to spawn on the other side, so how would I do it so the NPC spawns in an open space on the “side” where the player is in?
Also, how would I do it so the NPC spawns around the player?
game.ReplicatedStorage.AutoServing.CreateBot.OnServerEvent:Connect(function(player, data)
if data == 'HiddenKey01' then
local increaseCount = game.ReplicatedStorage.AutoServing.Current
local maxCount = game.ReplicatedStorage.AutoServing.MaxAI
if increaseCount.Value < maxCount.Value then
increaseCount.Value = increaseCount.Value + 1
local humanoidRootPart = workspace:FindFirstChild(player.Name).HumanoidRootPart
local bot = game.ReplicatedStorage.AutoServing.Chef:Clone()
bot.Parent = workspace
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Seated, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
bot.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Flying, false)
local pathfinding_service = game:GetService("PathfindingService")
local function checkDistance()
local distance = (bot.HumanoidRootPart.Position - humanoidRootPart.Position).Magnitude
--specify a radius/max distance
local radius = 10
--check to see if player is within radius
if distance <= radius then
bot.Humanoid.WalkSpeed = 0
game.ReplicatedStorage.AutoServing.ShowMenu:FireClient(player)
end
end
local function getClosestPlayer()
local closest_player, closest_distance = nil, 200 --Detection Distance
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and player ~= bot then
local distance = (bot.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
while true do
checkDistance()
local path = pathfinding_service:CreatePath()
local player, distance = getClosestPlayer()
if player and distance > 1 then
path:ComputeAsync(bot.HumanoidRootPart.Position, player.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
bot.Humanoid:MoveTo(waypoint.Position)
if waypoint.Action == Enum.PathWaypointAction.Jump then
bot:ChangeState(Enum.HumanoidStateType.Jumping)
end
while true do
local distance = (bot.PrimaryPart.Position - waypoint.Position).Magnitude
local distance_from_player = (bot.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < 5 then
break
end
if distance_from_player < 1 then
bot.Humanoid:MoveTo((bot.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
break
end
wait()
end
end
end
wait()
end
end
end
end)