So I have a toy following script thing. Whenever I create a toy. I do humRootPart:SetNetworkOwner(nil). After spawning in about 2-4 toys. It starts to get choppy again. Here’s the function for it.
`
function module.giveToy(toy, player)
if player == nil then return end
if 25 > player.ToyCount.Value then
local humRootPart = player.Character.HumanoidRootPart
local newToy = toy:Clone()
newToy.Parent = game.Workspace.Toys
newToy.HumanoidRootPart:SetNetworkOwner(nil)
local OwnerValue = newToy.Configuration.Modifiers.Owner
OwnerValue.Value = player
player.ToyCount.Value += 1
local humanoid = newToy.Humanoid
local IsDead = false
humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false) -- Reduce lag
humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Seated, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.Swimming, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.PlatformStanding, false)
-- Offsets and CFrames
local newOffset = CFrame.new(math.random(-5, 5), 0, math.random(5, 10))
ToyAnimatorModule:Animate(newToy)
wait(0.1)
local ToyTeleportAnimation = humanoid:LoadAnimation(humanoid.Parent.Configuration.Animations.Action.Teleport)
game.Players.PlayerRemoving:Connect(function(leavingplayer)
if leavingplayer == player then
newToy:Destroy()
end
end)
while wait() do
if newToy.Configuration then
if IsDead == false and newToy.Configuration.Modifiers.IsCollecting.Value == false then
if (humRootPart.Position - newToy.HumanoidRootPart.Position).magnitude > 30 then
newToy.HumanoidRootPart.CFrame = humRootPart.CFrame * newOffset
ToyTeleportAnimation:Play()
end
newToy.Humanoid:MoveTo((humRootPart.CFrame*newOffset).p)
end
OwnerValue.Value.Character.Humanoid.Died:Connect(function()
IsDead = true
player.CharacterAdded:Wait()
IsDead = false
humRootPart = player.Character.HumanoidRootPart
newToy.HumanoidRootPart.CFrame = humRootPart.CFrame * newOffset
end)
end
end
else
print("max toy limit reached")
end
end
If you think ToyAnimator is the problem. Here you go:
local module = {}
local vectorZero = Vector3.new(0, 0, 0)
local animatedBeings = {}
local toyDataModule = require(script.Parent.ToyData)
local function ifIdle(humanoid, NPC)
if NPC.HumanoidRootPart == nil then return end
if NPC.HumanoidRootPart.Velocity.X >= -5 and NPC.HumanoidRootPart.Velocity.X <= 5 and NPC.HumanoidRootPart.Velocity.Z >= -5 and NPC.HumanoidRootPart.Velocity.Z <= 5 then
return true
end
end
local constantAnimation = function(v)
if v == nil then return end
if v:GetState() == Enum.HumanoidStateType.Dead then
coroutine.yield()
end
local walkspeed = toyDataModule[v.Parent.Name].speed -- Get the data from the toydata module
local WalkAnimationId = toyDataModule[v.Parent.Name].animations.movement["Walk"]
local IdleAnimationId = toyDataModule[v.Parent.Name].animations.idle["Idle"]
local TeleportAnimationId = toyDataModule[v.Parent.Name].animations.action["Teleport"]
v.Parent.Configuration.Animations.Movement.Walk.AnimationId = WalkAnimationId
v.Parent.Configuration.Animations.Idle.Idle.AnimationId = IdleAnimationId
v.Parent.Configuration.Animations.Action.Teleport.AnimationId = TeleportAnimationId
local WalkAnimation = v:LoadAnimation(v.Parent.Configuration.Animations.Movement.Walk)
local IdleAnimation = v:LoadAnimation(v.Parent.Configuration.Animations.Idle.Idle)
game:GetService("RunService").Heartbeat:Connect(function()
if v.Parent then
if ifIdle(v, v.Parent) then
WalkAnimation:Stop()
if IdleAnimation.IsPlaying == false then
IdleAnimation:Play()
end
else
IdleAnimation:Stop()
if WalkAnimation.IsPlaying == false then
WalkAnimation:Play()
IdleAnimation:AdjustSpeed(v.WalkSpeed)
end
end
end
end)
end
function module:Animate(NPC)
local humanoid = NPC.Humanoid
spawn(function()
constantAnimation(humanoid)
end)
end
return module
`
Also, I made a script so that the toy can collect lego and stuff. When it walks to it. It also lags
Video: https://gyazo.com/03d56adb74d6ed87b7190585d9dc74a8?token=da096d8f9e2b36c4b79e0b47fe03d73c
Here is the code:
`
function module.CollectLego(toy, part, player)
if toy:IsA(“Model”) then
spawn(function()
LookAtPart(part, toy)
end)
local PositionOffset = Vector3.new(math.random(-2, 2), 0, math.random(-2, 2))
toy.Configuration.Modifiers.IsCollecting.Value = true
local debounce = false
while part.Configuration.Amount.Value > 0 do
wait()
toy.Humanoid:MoveTo(part.Position+PositionOffset)
if debounce == false then
spawn(function()
if debounce == false then
part.Configuration.Amount.Value -= 10
debounce = true
wait(1)
debounce = false
end
end)
end
end
toy.Configuration.Modifiers.IsCollecting.Value = false
end
end
`
ToyAnimator is a way to reduce lag. Because rather than using Roblox’s Animator script and changing some stuff. I wanted to do it from a centralized script. Btw I took the script from a dev forum: Centralising NPC animations into 1 simple core script? - #2 by Made_You but I fixed it and change stuff to my liking
Any help is appreciated.