-
What do you want to achieve? Keep it simple and clear!
Hello! I am a beginner scripter that is working on a survival colony simulator. I want to use a function from my ModuleScript on an array of dummies. -
What is the issue? Include screenshots / videos if possible!
The pathfinding gets executed, but I cannot find a way to use the function for all of them at once. What happens essentially is that the dummies start the path one by one, instead of all of them at once. The bug, of course, only occurs when the player has selected more than 1 dummy. -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried both using an array in the ServerScript and the ModuleScript. I also tried using another ModuleScript that activates the one with the pathfinding function. I’ve tried changing the ModuleScript’s parameters to the entire array, then used lots of for i, v in pairs() loops.
I have tried looking on the Developer Forum but never found anybody with a similar issue to mine.
This is the ServerScript (the function which calls the Module is in moveDummies):
local rs = game:GetService("ReplicatedStorage")
local spawnEvent = rs:WaitForChild("Spawn")
local selectEvent = rs:WaitForChild("Select")
local moveEvent = rs:WaitForChild("Move")
local attackEvent = rs:WaitForChild("Attack")
local mineEvent = rs:WaitForChild("Mine")
local pathfind = require(script.Pathfind)
local dummy = game.ServerStorage:WaitForChild("Dummy")
local playerName
local playerDummies = {}
local selectedDummy = {}
function moveDummies(pos)
print(#selectedDummy)
if #selectedDummy == 0 then return end
if #selectedDummy == 1 then pathfind.pathfind(selectedDummy, pos) return end
if #selectedDummy > 1 then
for i, v in pairs(selectedDummy) do
pathfind.pathfind(v, pos)
end
end
end
spawnEvent.OnServerEvent:Connect(function(plr, pos)
print("player: "..plr.Name..", pos: "..tostring(pos.Position))
local newDummy = dummy:Clone()
playerName = newDummy:WaitForChild("PlayerName")
playerName = plr.Name
newDummy.Name = plr.Name.."'s Dummy"
newDummy.Parent = workspace
newDummy.HumanoidRootPart.CFrame = CFrame.new(math.floor(pos.Position.X), pos.Position.Y, math.floor(pos.Position.Z))
newDummy:FindFirstChild("Humanoid"):ApplyDescription(game:GetService("Players"):GetHumanoidDescriptionFromUserId(plr.UserId))
table.insert(playerDummies, newDummy)
end)
selectEvent.OnServerEvent:Connect(function(plr, dummy)
print("selected "..dummy.Name)
dummy = dummy.Parent
table.insert(selectedDummy, dummy)
end)
moveEvent.OnServerEvent:Connect(function(plr, pos)
print("player: "..plr.Name..", pos: "..tostring(pos.Position))
moveDummies(pos)
selectedDummy = {}
end)
attackEvent.OnServerEvent:Connect(function(plr, target)
print("attacked")
selectedDummy = {}
end)
This is the ModuleScript:
local pathfindModule = {}
function pathfindModule.pathfind(dummy, finish)
print("module")
if not dummy:FindFirstChild("HumanoidRootPart") then return "no hrp" end
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentCanClimb = true,
WaypointSpacing = 2
})
local success, errorMessage = pcall(function()
print("pcall")
path:ComputeAsync(dummy.HumanoidRootPart.Position, finish.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
dummy.Humanoid:MoveTo(waypoint.Position)
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = workspace.Pathfind
if waypoint.Action == Enum.PathWaypointAction.Jump then
dummy.Humanoid.Jump = true
end
dummy.Humanoid.MoveToFinished:Wait()
continue
end
else
warn("path not found.")
end
end
return pathfindModule
I apologize if the video quality is low, I had to lower the bitrate of OBS to upload it directly to my topic.
Here is what happens: