Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script running very slow and lags game like crazy?

As like the title said. It's lagging very bad and slow, too.

Code:

function _FILLWATER(x, z)
	_CUMULATED = _CUMULATED + 1
	
	for _Y = _Y / _SCALE - 1, -_OFFSET, -1 do
		local _PART = Instance.new("Part")
		_PART.Anchored = true
		_PART.CanCollide = false
		_PART.BottomSurface = Enum.SurfaceType.Smooth
		_PART.CFrame = CFrame.new(x * _SCALE, (_Y * _SCALE) , z * _SCALE)
		_PART.formFactor = Enum.FormFactor.Symmetric
		_PART.Name = x ..'-water'
		_PART.Transparency = 0.5
		_PART.Size = Vector3.new(3, 3, 3)
		_PART.TopSurface = Enum.SurfaceType.Smooth
		_PART.Parent = _BIN
		_ALTDECALS(_PART)
		for i, v in pairs (_BIN:GetChildren()) do
			if v:IsA('Part') and v.Position == _PART.Position and v.Name ~= x ..'-water' then
				_PART:Destroy()
			end
		end

		if _CUMULATED % 32 == 0 then
			wait()
		end
	end
end

The code that actives it:

for _X = 1, _X do
	for _Z = 1, _Z do
		_FILLWATER(_X, _Z)
	end
end

1 comment

1

Try adding a small wait?

1 answer

✓ Accepted answer

The reason why your game is lagging so hard is because you don't have a wait() in your script where the water is being generated. In the only sequence where there is a wait() in your script, that sequence doesn't always happen. (It's impressive that the game isn't crashing though.) Without a wait, the loop relies on an extremely short delay time (I would say about .00000014 seconds or somewhere around that number) between actions, which causes lag and you have the chance of the game crashing.

I would put a wait() in both the loop and the function so that your game's performance improves.

This discussion is preserved as it was. New answers, comments, and votes are closed.