Hello, I am wondering how to make my enemys spawn client sided only, because it causes lag. I know its spawning server cause when I play test and switch to server theres still there. Heres the code:
------------------------------------
-- // Variables
local ServerStorage = game:GetService("ServerStorage")
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local mob = {}
-- // Move Function
function mob.Move(mob, map, startFrom)
local humanoid = mob:WaitForChild("Humanoid")
local waypoints = map.Waypoints
for waypoint = startFrom, #waypoints:GetChildren() do
mob.MovingTo.Value = waypoint
repeat
humanoid:MoveTo(waypoints[waypoint].Position)
until humanoid.MoveToFinished:Wait()
end
mob:Destroy()
end
-- // Spawning Function
function mob.Spawn(name, quantity, map, owner)
local mobExists = replicatedStorage.Mobs:FindFirstChild(name)
if mobExists then
for i =1, quantity do
task.wait()
local newMob = mobExists:Clone()
if owner ~= nil then
newMob.HumanoidRootPart.CFrame = owner.PrimaryPart.CFrame
else
newMob.HumanoidRootPart.CFrame = map.Start.CFrame
end
newMob.Parent = workspace.Mobs
newMob.HumanoidRootPart:SetNetworkOwner(nil)
local movingTo = Instance.new("IntValue")
movingTo.Name = "MovingTo"
movingTo.Parent = newMob
for i, object in ipairs(newMob:GetDescendants()) do
if object:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(object, "Mob")
end
end
newMob.Humanoid.Died:Connect(function()
task.wait(0.1)
newMob:Destroy()
end)
if owner ~= nil then
coroutine.wrap(mob.Move)(newMob, map, owner.MovingTo.Value)
else
coroutine.wrap(mob.Move)(newMob, map, 1)
end
end
else
warn("Requested mob does not exist:", name)
end
end
return mob