TweenService causing players to lag

I’m making a Obby game and its releasing this Saturday but during testing it on mobile i found that when the player touches a part that disappears and becomes not collidable the players device lags until the tween ends and when there is no tweens playing the game is smooth is there anyway to fix this?

Code (this is in a Server Script stored in ServerScriptService):

local CollectionService = game:GetService("CollectionService")


local Disappearparts = CollectionService:GetTagged("Disappear part")
local Disappearparts2 = CollectionService:GetTagged("Disappear part2")

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(1.5,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)
local info2 = TweenInfo.new(7,Enum.EasingStyle.Linear,Enum.EasingDirection.In,0,false,0)

local Table1 = {["Transparency"] = 1}
local Table2 = {["Transparency"] = 0}

for i, v in pairs(Disappearparts2) do
	if v:IsA("Part") then
		v.Touched:Connect(function(Touch)
			local Character = Touch.Parent
			if Character then
				if Character:FindFirstChild("Humanoid") then
					local Tween1 = tweenservice:Create(v,info2,Table1)
					Tween1:Play()
					Tween1.Completed:Connect(function()
						v.CanCollide = false
						task.wait(0.5)
						local Tween2 = tweenservice:Create(v,info,Table2)
						Tween2:Play()
						v.CanCollide = true
					end)
				end
			end
		end)
	end
end

for i, v in pairs(Disappearparts) do
	if v:IsA("Part") then
		v.Touched:Connect(function(Touch)
			local Character = Touch.Parent
			if Character then
				if Character:FindFirstChild("Humanoid") then
					local Tween1 = tweenservice:Create(v,info,Table1)
					Tween1:Play()
					Tween1.Completed:Connect(function()
						v.CanCollide = false
						task.wait(0.5)
						local Tween2 = tweenservice:Create(v,info,Table2)
						Tween2:Play()
						v.CanCollide = true
					end)
				end
			end
		end)
	end
end
1 Like

I’m new to tweens myself, but have you tried adding a debounce and maybe some debugging? It’s possible that the tween is running a lot of times which is causing the lag.

Tweens running on the server will always lag. If you made a system that has the tweens handled on the client, you probably won’t see as much, if any, lag.

1 Like

As said above, you need this on the client. Always handle particles, tweens, animations, etc. and other effects on the CLIENT.
You are stressing the server and instead of doing core functions, its instead using resources to replicate some effects for all other clients.

This should always be loaded for the client to handle individually!

Okay so i converted the script into a local script and now the tweens are moving more smoothly, however the main problem is still happening. on mobile devices when a tween plays the players device lags until the tween ends

It might be logical code which is causing this. Is there any problems with StreamingEnabled, and if so do load the client using the RequestStreamAroundAsync() (I think thats what it is called)

I fixed the problems with StreamingEnabled its just the players device lagging while it plays even with a debounce