While trying to make a tycoon with animated models as drops, i ran into a problem: periodically, every ~5-15 sec (there is no certain regularity) all animated models, moving by tweenservice on their HumanoidRootPart, got lagged for 1 sec, that looks like i got 5 fps. But other models, like my running character, or other moving usual NPCs (with Humanoid inside) dont get this kind of lags, so its not an fps drop. Important to know, that replacing models with just a part (and removing animations) still causes lags, but a little less.

This is a script each dropper has inside:

local ServerScriptService = game:GetService("ServerScriptService")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--// Base movespeed

Types = {Skeleton = 10, Zombie = 10, Ghost = 10}

--// Dropper as it is :)

Dropper = script.Parent

--// How much souls would spawned Creature contain ((base + add_from_upgraders) * multiplicate_from_upgraders)

function SoulsCount()
	local multiplicate = 1
	local add_amount = 0
	for _, v in pairs (Dropper.Parent:GetChildren()) do
		if v:FindFirstChild("Multiplicate") then
			if v.Multiplicate.Value == true then
				multiplicate = multiplicate * v.SoulsUpdate.Value
			else
				add_amount = add_amount + v.SoulsUpdate.Value
			end
		end
	end
	return (Dropper.Souls.Value+add_amount)*multiplicate
end

--// Way of created creature, based on waypoints (parts in dropper). If no waypoits, then 'way is end' and tycoon got souls, that player can collect.

function TweenWay (Corpse, Dropper, point, speed)						
	local WayPoint = Dropper:FindFirstChild("WayPoint"..point)
	if not WayPoint then
		if Corpse then
			Dropper.Parent.Parent.Parent.Souls.Value = Dropper.Parent.Parent.Parent.Souls.Value + Corpse.CreatureSouls.Value
			Corpse:remove()
		end
		return
	end
	local Time = (Corpse.HumanoidRootPart.Position - WayPoint.Position).magnitude/speed
	local Way = TweenInfo.new(
	Time, --?????
	Enum.EasingStyle.Linear
	)

	local tween = TweenService:Create(Corpse.HumanoidRootPart, Way, {CFrame = WayPoint.CFrame})
	
	if WayPoint:FindFirstChild("Anim") then
		local Animation = game.ReplicatedStorage.Animations:FindFirstChild(Dropper.Type.Value..WayPoint.Anim.Value)							
		local LoadAnimation = Corpse.AnimationController:LoadAnimation(Animation)
		if WayPoint.Anim:FindFirstChild("Unique") then
			local Aspeed = LoadAnimation.Length/(Time*1.05)
			LoadAnimation:Play()
			LoadAnimation:AdjustSpeed(Aspeed)
		else
			LoadAnimation:Play()
		end			
	end
	
	tween:Play()
	local Point = point+1
	tween.Completed:Wait(15)
	TweenWay(Corpse, Dropper, Point, speed)
	return									
end

--// Loop for spawn creatures. Calls SoulCount and TweenWay functions. Wait() randomize for spawns not to be at the same time to resduce lags (is it a bad idea?)

while true do
	wait(Dropper.Cooldown.Value+math.random(-20,20)*0.01)
	local Corpse = ReplicatedStorage:FindFirstChild(script.Parent.Type.Value):Clone()
	Corpse.Parent = Dropper.Parent.Parent.Parent.Creatures
	Corpse:FindFirstChild("HumanoidRootPart").CFrame = Dropper.Drop.CFrame
	Corpse.Name = "Creature"
	Corpse.CreatureSouls.Value = SoulsCount()
	local Speed = Types[Dropper.Type.Value]
	TweenWay(Corpse, Dropper, 1, Speed)
end

So, whats the worst mistakes in my script and what can cause that lags?

4 comments

0

This may not completely remove the lag, but you are using deprecated functions. Replace :remove() with :Destroy()

0

To much anything can be bad, so yes to much Tween could make your game laggy (or i think so, idk)

0

So what is the best way to replace Tween in this case? I even thought about spawning creatures with humanoid, that would really walk, and change their Collision Group, but i think it's even worse for server, and i have no idea how to make flying creatures. Anyway, is there tycoons in roblox with animated drops? If yes, then there is a way to do it at least.

0

To be honest, I dont think tweenservice can cause lag it depends on the amount of parts and scripts.