I have a feature in my game where customers will enter the store and walk around, looking for an item. When i tell the customer to walk to a random position, it will go straight there and climb the shelves with AgentCanClimb equal to false. How can i fix this and make the NPC take a reasonable path?
(I set the humanoid’s climbing StateEnabled to false as an attempt to fix this)
You can make a part and randomise its position in the store, then check if its on a shelf, if it is, run the part-making process again until its on the ground, and make the npc move to that part.
Also the NPC is not climbing, but just stepping onto a brick. Not really climbing but close enough.
The issue is not that the position the NPC moves to is on the shelf, its that the NPC is making a path that goes over the shelf. I already have my positions in a table, and it picks a random one from that table.
local SS = game:GetService("ServerStorage")
local Sounds = game:GetService("SoundService")
local CS = game:GetService("Chat")
local PFS = game:GetService("PathfindingService")
local TS = game:GetService("TweenService")
local customer = SS:WaitForChild("CustomerNPC")
local hum = customer:WaitForChild("Humanoid")
local animator = hum.Animator
local hrp = customer:WaitForChild("HumanoidRootPart")
local head = customer:WaitForChild("Head")
local thinkAnim = script.Think
local thinkTrack = animator:LoadAnimation(thinkAnim)
local positionsFolder = game.Workspace.CustomerLocations
local randFolder = positionsFolder.RandomSpots
local randPos = randFolder:GetChildren()
local path = PFS:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentCanClimb = false,
WaypointSpacing = 4,
})
local function tweenPart(part, cframe, tweenTime)
TS:Create(part, TweenInfo.new(tweenTime), {CFrame = cframe}):Play()
end
local function moveNPC(destination)
hum:MoveTo(destination)
-- Repeat check every timeout seconds to see if NPC reached destination
while (customer.PrimaryPart.Position - destination).Magnitude > 1 do
wait(5)
hum:MoveTo(destination)
end
tweenPart(hrp, destination, 0.5)
end
local function chat(msg)
CS:Chat(head, msg, Enum.ChatColor.White)
end
local function lookForItem()
local count = math.random(2, #randPos)
for i = 1, count do
local posDecided = randPos[math.random(1, #randPos)]
path:ComputeAsync(hrp.Position, posDecided.Position)
for i, point in path:GetWaypoints() do
hum:MoveTo(point.Position)
hum.MoveToFinished:Wait()
end
wait()
tweenPart(hrp, posDecided.CFrame, 0.5)
thinkTrack:Play()
wait(math.random(4, 10))
thinkTrack:Stop()
end
return
end
while wait(math.random(5, 20)) do
Sounds.Chime:Play()
customer.Parent = game.Workspace
hrp.CFrame = positionsFolder.SpawnPart.CFrame
hrp:SetNetworkOwner(nil)
lookForItem()
moveNPC(positionsFolder.RingPart.Position)
end