#How can i opmize this script in a way that 2000+ characters can be on the path?
1 messages · Page 1 of 1 (latest)
local PathFindingService = game:GetService("PathfindingService")
-- Module table to hold NPC movement functions
local Module = {}
-- Load configuration from ServerStorage
local Config = require(game:GetService("ServerStorage").GameConfig.ConfigGeral)
-- Maximum number of spins a character can do on the track
local MaxSpins:number = 1
-- The checkpoint number that triggers the end of a spin
local EnterNumber = Config.enternumber
-- Table to track which NPCs can move
local CanMove = {}
-- References to the track folder in workspace
local TrackFolder = workspace["Important Parts"].Track
local EndPath = TrackFolder.EndPath -- The final path points for NPCs
local Path = TrackFolder.TrackPath -- The main track points
local PathChild = Path:GetChildren()
local EndPathChiald = EndPath:GetChildren()
-- Function to move a humanoid to a position and wait until it finishes
local function MoveToAndWait(hum, pos)
hum:MoveTo(pos)
hum.MoveToFinished:Wait()
end
-- Function to move an NPC along the track
-- NPC: The NPC model
-- ID: Unique identifier for the NPC (used in CanMove table)
function Module.NpcMove(NPC: Model, ID,plr)
-- Enable movement for this NPC
if not CanMove[ID] then
CanMove[ID] = true
end
-- Get the humanoid of the NPC
local hum = NPC:FindFirstChildOfClass("Humanoid")
if not hum then return end
local spins = 0 -- How many complete loops the NPC has done
local checkpoint = 1 -- Current point in the track
if plr then
MaxSpins = Config.maxspinsPlayer
else
MaxSpins = Config.maxspins
end
-- Set the humanoid's walk speed based on configuration
hum.WalkSpeed = Config.BeltSpeed
-- Spawn a task to move the NPC along the track
local taskA = task.spawn(function()
while CanMove[ID] do
if spins >= MaxSpins and checkpoint == EnterNumber then
-- Move through all EndPath points before destroying the NPC
for _, point in ipairs(EndPathChiald) do
MoveToAndWait(hum, point.Position)
end
NPC:Destroy()
break
end
-- Move to the next checkpoint
local point = Path:FindFirstChild("Point"..checkpoint)
MoveToAndWait(hum, point.Position)
-- Increment checkpoint
checkpoint += 1
-- If the NPC passed the last checkpoint, reset it and increment spins
if checkpoint > #PathChild then
checkpoint = Config.CheckpointReset
spins += 1
end
end
end)
end
-- Function to stop an NPC's movement
-- Model: The NPC model
-- ID: Unique identifier for the NPC
function Module.Stop(Model: Model, ID)
if CanMove[ID] then
CanMove[ID] = false
end
end
-- Return the Module table
return Module```
Well in my opinion :Moveto is limiting you since asking for 2000+ entities on the server is just going to lag it a lot.
I would probably make it a limiter on how much NPCS can be on the server at once and only use this script when a player is in a decent stud radius.
Other than that unless i'm missing something you should be good
- dont use ai
- prob scrap that tbf
you should render it on client only
that moving is heavy
your network is gonna be like 1mbps
terrible
so if one path just compute path once
serialize
send to client to follow
OR have server use MATH (no actual instances) to guess where its supposed to be
and replicate to client with buffer
I thought the excessive commenting was strange
I did by my self and asked ia to document, it's for an job
You want 2000+ characters to be on the path?Simple, hide the characters that arent visible to the camera, hide the characters that are outside the render distance
If this doesnt work, you have to get help somewhere else