Hi devs so im facing a problem here so I want to make it so that the Dummy spawns from Replicated Storage to a specific parts location in workspace and then after that it moves to any random seat placed in the map and the Dummys/NPC are suppose to spawn every certain amount of time the problem is It spawns but it doesnt move at all Here’s the code if anyone can guide me on any mistakes I made Thank you.
----//Service
----//Irretation of the Seats Dictionary
local Dummys = {
game.ReplicatedStorage.NPCs.TestDummy
}
local Seats = {
workspace.NPCSEATS.Seat,
workspace.NPCSEATS.Seat2,
workspace.NPCSEATS.Seat3
}
local DummySpawnPoint = workspace.NPCSEATS.NPCSpawnPoint.NPCSPAWN
---------------------------------------------------NPC SPAWNING CONTROLLER----------------------------------------------------------
local SpawnNPCs = function()
local RandomDummy = math.random(1,#Dummys)
local Dummy = Dummys[RandomDummy]
local ClonedDummy = Dummy:Clone()
ClonedDummy.Parent = game.Workspace
Dummy.PrimaryPart.Position = DummySpawnPoint.Position
end
local SpawnTimer = 0
while true do
wait(5)
SpawnTimer = SpawnTimer + 5
if SpawnTimer >= 30 then
SpawnTimer = 0
end
if SpawnTimer % 5 == 0 then
SpawnNPCs()
end
end
--------------------------------------------NPC MOVEMENT SECTION--------------------------------------------------------------------
local Seats = {
workspace.NPCSEATS.Seat,
workspace.NPCSEATS.Seat2,
workspace.NPCSEATS.Seat3
}
----//VARIABLES
local NPCCharacters= game.Workspace:WaitForChild("TestDummy")
local NPCs = Dummys[NPCCharacters]
local HRP = NPCs:WaitForChild("HumanoidRootPart")
local closesSeat = nil
local closestSeatDistance = math.huge
----//Irriterate through the Seats Dictionay and calculates the Distance from the Model
local success, errormessage = pcall(function()
for _,seat in ipairs(Seats) do
local distance = (seat.Position - NPCs.HumanoidRootPart).Magnitude
if distance < closestSeatDistance then
closesSeat = seat
closestSeatDistance = distance
end
--//Calcs if Seat has occupant
if Seats.Occupant ~= nil then
closesSeat = nil
end
end --End of for loop
if closesSeat then
local humanoid =NPCs:FindFirstChild("Humanoid")
--RandomSitSelec
local RandomSeats = math.random(1,#Seats)
closesSeat = Seats[RandomSeats]
humanoid:MoveTo(closesSeat.Position)
humanoid:Sit(closesSeat)
end
end) --End of pcall
if success then
print("NPC INIT! ")
else
print("NPC INIT! ")
end
This right here is a big problem. Instead of skipping the seat if it is occupied, you set the closesSeat to nil, which means if the closest chair is occupied, then nothing will happen. I suggest you use this code instead:
for _,seat in ipairs(Seats) do
if Seats.Occupant then continue end --//Calcs if Seat has occupant
local distance = (seat.Position - NPCs.HumanoidRootPart).Magnitude
if distance < closestSeatDistance then
closesSeat = seat
closestSeatDistance = distance
end
end --End of for loop
This part here, why are you deciding a random seat? What was the point of calculating the closest seat if you just replace it again?
_
Here you make the NPC start moving towards it, but you never actually wait for it to finish moving, so it will instantly teleport to the seat.
_
And this is the main issue. You have a while true loop which will indefinitely hold, and never let the rest of the code run.
Okay after reviewing this I change some stuff cause Indeed I made a mistake by first calculating the distance of the chairs so I want the NPC to move at Random Seats…And also How do you suggest I wait for the NPC to finish moving I researched a bit a found something called MoveToFinish()…and lastly when I remove the while loop the NPC spawns once.Here’s the edited code
----//Service
----//Irretation of the Seats Dictionary
local Dummys = {
game.ReplicatedStorage.NPCs.TestDummy
}
local Seats = {
workspace.NPCSEATS.Seat,
workspace.NPCSEATS.Seat2,
workspace.NPCSEATS.Seat3
}
local DummySpawnPoint = workspace.NPCSEATS.NPCSpawnPoint.NPCSPAWN
---------------------------------------------------NPC SPAWNING CONTROLLER----------------------------------------------------------
local SpawnNPCs = function()
local RandomDummy = math.random(1,#Dummys)
local Dummy = Dummys[RandomDummy]
local ClonedDummy = Dummy:Clone()
ClonedDummy.Parent = game.Workspace
Dummy.PrimaryPart.Position = DummySpawnPoint.Position
end
local SpawnTimer = 0
wait(5)
SpawnTimer = SpawnTimer + 5
if SpawnTimer >= 30 then
SpawnTimer = 0
end
if SpawnTimer % 5 == 0 then
SpawnNPCs()
end
--------------------------------------------NPC MOVEMENT SECTION--------------------------------------------------------------------
local Seats = {
workspace.NPCSEATS.Seat,
workspace.NPCSEATS.Seat2,
workspace.NPCSEATS.Seat3
}
----//VARIABLES
local NPCCharacters= game.Workspace:WaitForChild("TestDummy")
local NPCs = Dummys[NPCCharacters]
local HRP = NPCs:WaitForChild("HumanoidRootPart")
local humanoid =NPCs:FindFirstChild("Humanoid")
local closesSeat = nil
local closestSeatDistance = math.huge
----//Irriterate through the Seats Dictionay and calculates the Distance from the Model
local success, errormessage = pcall(function()
--RandomSitSelec
local RandomSeats = math.random(1,#Seats)
closesSeat = Seats[RandomSeats]
humanoid:MoveTo(closesSeat.Position)
humanoid:Sit(closesSeat)
end) --End of pcall
if success then
print("NPC INIT! ")
else
print("NPC INIT! ")
end
For the waiting part, you can use humanoid:MoveToFinished:Wait()
I also recommend you check if the seats are occupied. Unless players are going to sit on the seats, you might as well remove the randomly picked seat from the table.
As for the spawning, you can have a while loop if you don’t run any code after it. When spawning the NPC you need to run the movement code, but to not yield the loop, you need to use the spawn function.
Ok So I did it and There’s no movement from the NPC still but it spawns so this means the problem has to come from the NPC going to a Random seat found in the Dictionary of Seats I wrapped it in a pcall function it gives me no error I just don’t know…