Part growing slow

For some reason my part is growing slowly, when it should all be growing at the same time, does anyone know how to fix this? It’s supposed to grow once every second, and that’s working perfectly fine.

local function Add(numb1,numb2)
	local result = numb1
	for i = 1,numb2 do
		numb1 = numb1 + 1
		wait(0.1) --To let it think
	end
	script.Parent:SetAttribute("Result",numb1)
	game.ReplicatedStorage.RemoteEvent:FireAllClients(script.Parent:GetAttribute("Result"),numb1,numb2)
	
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Wait()
end

local function WaitForChild(item,name)
	local child = item:FindFirstChild(name)
	
	if child then return child end
	
	while true do
		child = item:FindFirstChild(name)
		if child then return child end
		item.ChildAdded:Wait()
	end
end

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,numb,result)
	if numb == result then
			
	else
		while true do --Punish it so it can learn
			wait()
			local workspace = WaitForChild(script:FindFirstAncestorOfClass("DataModel"),"Workspace")
			local newPart = Instance.new("Part",script:FindFirstAncestorOfClass("DataModel"):FindFirstChild(workspace.Name))
		end
	end
end)

while true do
	Add(script.Parent.Size.X, 1)
	script.Parent.Size = Vector3.new(script.Parent:GetAttribute("Result"),script.Parent.Size.Y,script.Parent.Size.Z)

	Add(script.Parent.Size.Y, 1)
	script.Parent.Size = Vector3.new(script.Parent.Size.X,script.Parent:GetAttribute("Result"),script.Parent.Size.Z)

	Add(script.Parent.Size.Z, 1)
	script.Parent.Size = Vector3.new(script.Parent.Size.X,script.Parent.Size.Y,script.Parent:GetAttribute("Result"))

	wait(1)
end
1 Like

The problem is that the for loop in the add function is yielding the while true do loop.

So you should remove the wait(0.1) from the loop, so it grows all at the same time.
Another tip is that you could use task.wait() which is more accurate.

That certainly helped, but at some point it gets super slow, do you know why?

1 Like

Based on the code you sent there should not be any problem, if you are able to record it would be helpful

this adds delay also. I assume it waits for any first client that fires the event.

1 Like


I’m very confused because it’s working fully as intended with no errors and nothing to slow it down

1 Like

I mean, whats that giant shadow that is appearing?
Maybeeee thats the problem.

Your code is not optimized to be handling a lot of parts, and its not ideal to have 1 script in each part, plus a while loop attatched to each of it.

That’s to teach it not to make mistakes and learn, it wouldn’t cause it to slow as it doesn’t tell it to

The Roblox physics engine is kinda bad, and it dosent support a lot of instances being affected by it, this will cause lag to the server and because your script is a server script you are basically lagging the server, which makes scripts slower.

I doubt that’s it, but thank you for the suggestion.

Since it seems to require precision for the part to grow, it’d be better to utilize task.wait rather than wait(n). As @af_2048 had also mentioned, you’re making it wait for a response from the client, which I’m not sure the purpose of.

This may better accomplish the resize goal.

local function IncrementSize(Part, StartSize, Increment)
	local TweenTime = math.floor(Increment.X ~= 0 and Increment.X or Increment.Y ~= 0 and Increment.Y or Increment.Z ~= 0 and Increment.Z) * 0.1
	TweenService:Create(Part, TweenInfo.new(TweenTime), {Size = StartSize + Increment}):Play()
end

IncrementSize(Workspace.Part, Workspace.Part.Size, Vector3.new(0, 1, 1))